mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-07 23:41:22 +08:00
fix a4ce29e656: make variables independent on order
This commit is contained in:
@@ -24,20 +24,23 @@ class SessionContext(object):
|
|||||||
variables_mapping will be evaluated first.
|
variables_mapping will be evaluated first.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
variables_mapping (dict/list)
|
variables_mapping (dict)
|
||||||
[
|
{
|
||||||
{"TOKEN": "debugtalk"},
|
"random": "${gen_random_string(5)}",
|
||||||
{"random": "${gen_random_string(5)}"},
|
"authorization": "${gen_md5($TOKEN, $data, $random)}",
|
||||||
{"data": '{"name": "user", "password": "123456"}'},
|
"data": '{"name": "user", "password": "123456"}',
|
||||||
{"authorization": "${gen_md5($TOKEN, $data, $random)}"}
|
"TOKEN": "debugtalk",
|
||||||
]
|
}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.test_variables_mapping = {}
|
|
||||||
self.test_variables_mapping.update(self.session_variables_mapping)
|
|
||||||
|
|
||||||
variables_mapping = variables_mapping or {}
|
variables_mapping = variables_mapping or {}
|
||||||
variables_mapping = utils.ensure_mapping_format(variables_mapping)
|
variables_mapping = utils.ensure_mapping_format(variables_mapping)
|
||||||
|
|
||||||
|
self.test_variables_mapping = {}
|
||||||
|
# priority: extracted variable > teststep variable
|
||||||
|
self.test_variables_mapping.update(variables_mapping)
|
||||||
|
self.test_variables_mapping.update(self.session_variables_mapping)
|
||||||
|
|
||||||
for variable_name, variable_value in variables_mapping.items():
|
for variable_name, variable_value in variables_mapping.items():
|
||||||
variable_value = self.eval_content(variable_value)
|
variable_value = self.eval_content(variable_value)
|
||||||
self.update_test_variables(variable_name, variable_value)
|
self.update_test_variables(variable_name, variable_value)
|
||||||
|
|||||||
@@ -767,12 +767,6 @@ def __parse_tests(tests, config, project_mapping):
|
|||||||
test_dict.pop("variables", {}),
|
test_dict.pop("variables", {}),
|
||||||
config_variables
|
config_variables
|
||||||
)
|
)
|
||||||
# parse test variables with config variables
|
|
||||||
test_dict["variables"] = parse_data(
|
|
||||||
test_dict["variables"],
|
|
||||||
test_dict["variables"],
|
|
||||||
functions
|
|
||||||
)
|
|
||||||
|
|
||||||
# parse test_dict name
|
# parse test_dict name
|
||||||
try:
|
try:
|
||||||
@@ -802,12 +796,6 @@ def __parse_tests(tests, config, project_mapping):
|
|||||||
test_dict.pop("variables", {}),
|
test_dict.pop("variables", {}),
|
||||||
config_variables
|
config_variables
|
||||||
)
|
)
|
||||||
# parse test variables with config variables
|
|
||||||
test_dict["variables"] = parse_data(
|
|
||||||
test_dict["variables"],
|
|
||||||
test_dict["variables"],
|
|
||||||
functions
|
|
||||||
)
|
|
||||||
|
|
||||||
# parse test_dict name
|
# parse test_dict name
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -286,13 +286,15 @@ def ensure_mapping_format(variables):
|
|||||||
variables_ordered_dict = {}
|
variables_ordered_dict = {}
|
||||||
for map_dict in variables:
|
for map_dict in variables:
|
||||||
variables_ordered_dict.update(map_dict)
|
variables_ordered_dict.update(map_dict)
|
||||||
|
|
||||||
|
return variables_ordered_dict
|
||||||
|
|
||||||
elif isinstance(variables, dict):
|
elif isinstance(variables, dict):
|
||||||
variables_ordered_dict = variables
|
return variables
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise exceptions.ParamsError("variables format error!")
|
raise exceptions.ParamsError("variables format error!")
|
||||||
|
|
||||||
return variables_ordered_dict
|
|
||||||
|
|
||||||
|
|
||||||
def _convert_validators_to_mapping(validators):
|
def _convert_validators_to_mapping(validators):
|
||||||
""" convert validators list to mapping.
|
""" convert validators list to mapping.
|
||||||
|
|||||||
@@ -22,12 +22,27 @@ class TestContext(ApiServerUnittest):
|
|||||||
context_functions = self.context.FUNCTIONS_MAPPING
|
context_functions = self.context.FUNCTIONS_MAPPING
|
||||||
self.assertIn("gen_md5", context_functions)
|
self.assertIn("gen_md5", context_functions)
|
||||||
|
|
||||||
def test_init_test_variables(self):
|
def test_init_test_variables_initialize(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.context.test_variables_mapping,
|
self.context.test_variables_mapping,
|
||||||
{'SECRET_KEY': 'DebugTalk'}
|
{'SECRET_KEY': 'DebugTalk'}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_init_test_variables(self):
|
||||||
|
variables = {
|
||||||
|
"random": "${gen_random_string($num)}",
|
||||||
|
"authorization": "${gen_md5($TOKEN, $data, $random)}",
|
||||||
|
"data": '{"name": "$username", "password": "123456"}',
|
||||||
|
"TOKEN": "debugtalk",
|
||||||
|
"username": "user1",
|
||||||
|
"num": 6
|
||||||
|
}
|
||||||
|
self.context.init_test_variables(variables)
|
||||||
|
variables_mapping = self.context.test_variables_mapping
|
||||||
|
self.assertEqual(len(variables_mapping["random"]), 6)
|
||||||
|
self.assertEqual(len(variables_mapping["authorization"]), 32)
|
||||||
|
self.assertEqual(variables_mapping["data"], '{"name": "user1", "password": "123456"}')
|
||||||
|
|
||||||
def test_update_seesion_variables(self):
|
def test_update_seesion_variables(self):
|
||||||
self.context.update_seesion_variables({"TOKEN": "debugtalk"})
|
self.context.update_seesion_variables({"TOKEN": "debugtalk"})
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
@@ -57,12 +72,13 @@ class TestContext(ApiServerUnittest):
|
|||||||
# )
|
# )
|
||||||
|
|
||||||
def test_get_parsed_request(self):
|
def test_get_parsed_request(self):
|
||||||
variables = [
|
variables = {
|
||||||
{"TOKEN": "debugtalk"},
|
"random": "${gen_random_string(5)}",
|
||||||
{"random": "${gen_random_string(5)}"},
|
"data": '{"name": "user", "password": "123456"}',
|
||||||
{"data": '{"name": "user", "password": "123456"}'},
|
"authorization": "${gen_md5($TOKEN, $data, $random)}",
|
||||||
{"authorization": "${gen_md5($TOKEN, $data, $random)}"}
|
"TOKEN": "debugtalk"
|
||||||
]
|
}
|
||||||
|
|
||||||
self.context.init_test_variables(variables)
|
self.context.init_test_variables(variables)
|
||||||
|
|
||||||
request = {
|
request = {
|
||||||
@@ -82,7 +98,10 @@ class TestContext(ApiServerUnittest):
|
|||||||
self.assertIn("random", parsed_request["headers"])
|
self.assertIn("random", parsed_request["headers"])
|
||||||
self.assertEqual(len(parsed_request["headers"]["random"]), 5)
|
self.assertEqual(len(parsed_request["headers"]["random"]), 5)
|
||||||
self.assertIn("data", parsed_request)
|
self.assertIn("data", parsed_request)
|
||||||
self.assertEqual(parsed_request["data"], variables[2]["data"])
|
self.assertEqual(
|
||||||
|
parsed_request["data"],
|
||||||
|
'{"name": "user", "password": "123456"}'
|
||||||
|
)
|
||||||
self.assertEqual(parsed_request["headers"]["secret_key"], "DebugTalk")
|
self.assertEqual(parsed_request["headers"]["secret_key"], "DebugTalk")
|
||||||
|
|
||||||
def test_do_validation(self):
|
def test_do_validation(self):
|
||||||
@@ -106,10 +125,11 @@ class TestContext(ApiServerUnittest):
|
|||||||
{"check": "$resp_status_code", "comparator": "eq", "expect": 201},
|
{"check": "$resp_status_code", "comparator": "eq", "expect": 201},
|
||||||
{"check": "$resp_body_success", "comparator": "eq", "expect": True}
|
{"check": "$resp_body_success", "comparator": "eq", "expect": True}
|
||||||
]
|
]
|
||||||
variables = [
|
variables = {
|
||||||
{"resp_status_code": 200},
|
"resp_status_code": 200,
|
||||||
{"resp_body_success": True}
|
"resp_body_success": True
|
||||||
]
|
}
|
||||||
|
|
||||||
self.context.init_test_variables(variables)
|
self.context.init_test_variables(variables)
|
||||||
|
|
||||||
with self.assertRaises(exceptions.ValidationFailure):
|
with self.assertRaises(exceptions.ValidationFailure):
|
||||||
|
|||||||
@@ -452,7 +452,7 @@ class TestParser(unittest.TestCase):
|
|||||||
# self.assertEqual(len(parsed_testcases), 2 * 2)
|
# self.assertEqual(len(parsed_testcases), 2 * 2)
|
||||||
self.assertEqual(parsed_testcases[0]["config"]["name"], '1230')
|
self.assertEqual(parsed_testcases[0]["config"]["name"], '1230')
|
||||||
|
|
||||||
def test_parse_tests_fix_override_variables(self):
|
def test_parse_tests_override_variables(self):
|
||||||
tests_mapping = {
|
tests_mapping = {
|
||||||
'testcases': [
|
'testcases': [
|
||||||
{
|
{
|
||||||
@@ -479,7 +479,7 @@ class TestParser(unittest.TestCase):
|
|||||||
parser.parse_tests(tests_mapping)
|
parser.parse_tests(tests_mapping)
|
||||||
test_dict1_variables = tests_mapping["testcases"][0]["tests"][0]["variables"]
|
test_dict1_variables = tests_mapping["testcases"][0]["tests"][0]["variables"]
|
||||||
self.assertEqual(test_dict1_variables["creator"], "user_test_001")
|
self.assertEqual(test_dict1_variables["creator"], "user_test_001")
|
||||||
self.assertEqual(test_dict1_variables["username"], "user_test_001")
|
self.assertEqual(test_dict1_variables["username"], "$creator")
|
||||||
|
|
||||||
def test_parse_environ(self):
|
def test_parse_environ(self):
|
||||||
os.environ["PROJECT_KEY"] = "ABCDEFGH"
|
os.environ["PROJECT_KEY"] = "ABCDEFGH"
|
||||||
|
|||||||
Reference in New Issue
Block a user