mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 07:26:55 +08:00
bind_variables: variable_binds now can be ordered dict
This commit is contained in:
+18
-17
@@ -54,7 +54,7 @@ class Context(object):
|
|||||||
or config_dict.get('import_module_functions', [])
|
or config_dict.get('import_module_functions', [])
|
||||||
self.import_module_items(module_items, level)
|
self.import_module_items(module_items, level)
|
||||||
|
|
||||||
variable_binds = config_dict.get('variable_binds', [])
|
variable_binds = config_dict.get('variable_binds', OrderedDict())
|
||||||
self.bind_variables(variable_binds, level)
|
self.bind_variables(variable_binds, level)
|
||||||
|
|
||||||
def import_requires(self, modules):
|
def import_requires(self, modules):
|
||||||
@@ -90,32 +90,33 @@ class Context(object):
|
|||||||
self.__update_context_functions_config(level, imported_functions_dict)
|
self.__update_context_functions_config(level, imported_functions_dict)
|
||||||
|
|
||||||
imported_variables_dict = utils.filter_module(imported_module, "variable")
|
imported_variables_dict = utils.filter_module(imported_module, "variable")
|
||||||
variable_binds = [{key: value} for key, value in imported_variables_dict.items()]
|
self.bind_variables(imported_variables_dict, level)
|
||||||
self.bind_variables(variable_binds, level)
|
|
||||||
|
|
||||||
def bind_variables(self, variable_binds, level="testcase"):
|
def bind_variables(self, variable_binds, level="testcase"):
|
||||||
""" bind variables to testset context or current testcase context.
|
""" bind variables to testset context or current testcase context.
|
||||||
variables in testset context can be used in all testcases of current test suite.
|
variables in testset context can be used in all testcases of current test suite.
|
||||||
|
|
||||||
@param (list) variable_binds, variable can be value or custom function.
|
@param (list or OrderDict) variable_binds, variable can be value or custom function.
|
||||||
if value is function, it will be called and bind result to variable.
|
if value is function, it will be called and bind result to variable.
|
||||||
e.g.
|
e.g.
|
||||||
[
|
OrderDict({
|
||||||
{"TOKEN": "debugtalk"},
|
"TOKEN": "debugtalk",
|
||||||
{"random": "${gen_random_string(5)}"},
|
"random": "${gen_random_string(5)}",
|
||||||
{"json": {'name': 'user', 'password': '123456'}},
|
"json": {'name': 'user', 'password': '123456'},
|
||||||
{"md5": "${gen_md5($TOKEN, $json, $random)}"}
|
"md5": "${gen_md5($TOKEN, $json, $random)}"
|
||||||
]
|
})
|
||||||
"""
|
"""
|
||||||
for variable_bind in variable_binds:
|
if isinstance(variable_binds, list):
|
||||||
for variable_name, value in variable_bind.items():
|
variable_binds = utils.convert_to_order_dict(variable_binds)
|
||||||
variable_evale_value = self.testcase_parser.parse_content_with_bindings(value)
|
|
||||||
|
|
||||||
if level == "testset":
|
for variable_name, value in variable_binds.items():
|
||||||
self.testset_shared_variables_mapping[variable_name] = variable_evale_value
|
variable_evale_value = self.testcase_parser.parse_content_with_bindings(value)
|
||||||
|
|
||||||
self.testcase_variables_mapping[variable_name] = variable_evale_value
|
if level == "testset":
|
||||||
self.testcase_parser.bind_variables(self.testcase_variables_mapping)
|
self.testset_shared_variables_mapping[variable_name] = variable_evale_value
|
||||||
|
|
||||||
|
self.testcase_variables_mapping[variable_name] = variable_evale_value
|
||||||
|
self.testcase_parser.bind_variables(self.testcase_variables_mapping)
|
||||||
|
|
||||||
def __update_context_functions_config(self, level, config_mapping):
|
def __update_context_functions_config(self, level, config_mapping):
|
||||||
"""
|
"""
|
||||||
|
|||||||
+3
-13
@@ -114,8 +114,8 @@ class Runner(object):
|
|||||||
resp = self.http_client_session.request(url=url, method=method, **parsed_request)
|
resp = self.http_client_session.request(url=url, method=method, **parsed_request)
|
||||||
resp_obj = response.ResponseObject(resp)
|
resp_obj = response.ResponseObject(resp)
|
||||||
|
|
||||||
extracted_variables_mapping_list = resp_obj.extract_response(extract_binds)
|
extracted_variables_mapping = resp_obj.extract_response(extract_binds)
|
||||||
self.context.bind_variables(extracted_variables_mapping_list, level="testset")
|
self.context.bind_variables(extracted_variables_mapping, level="testset")
|
||||||
|
|
||||||
resp_obj.validate(validators, self.context.get_testcase_variables_mapping())
|
resp_obj.validate(validators, self.context.get_testcase_variables_mapping())
|
||||||
|
|
||||||
@@ -159,19 +159,9 @@ class Runner(object):
|
|||||||
success = True
|
success = True
|
||||||
config_dict = testset.get("config", {})
|
config_dict = testset.get("config", {})
|
||||||
|
|
||||||
def merge_variable_binds(variable_binds, variables_mapping):
|
|
||||||
variables_dict = OrderedDict()
|
|
||||||
for variable_dict in variable_binds:
|
|
||||||
variables_dict.update(variable_dict)
|
|
||||||
|
|
||||||
for var, value in variables_mapping.items():
|
|
||||||
variables_dict.update({var: value})
|
|
||||||
|
|
||||||
return [{var: value} for var, value in variables_dict.items()]
|
|
||||||
|
|
||||||
variable_binds = config_dict.get("variable_binds", [])
|
variable_binds = config_dict.get("variable_binds", [])
|
||||||
variables_mapping = variables_mapping or {}
|
variables_mapping = variables_mapping or {}
|
||||||
config_dict["variable_binds"] = merge_variable_binds(variable_binds, variables_mapping)
|
config_dict["variable_binds"] = utils.override_variables_binds(variable_binds, variables_mapping)
|
||||||
|
|
||||||
self.init_config(config_dict, level="testset")
|
self.init_config(config_dict, level="testset")
|
||||||
testcases = testset.get("testcases", [])
|
testcases = testset.get("testcases", [])
|
||||||
|
|||||||
Reference in New Issue
Block a user