mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-27 11:10:31 +08:00
bind_extractors: Bind named extractors to value within the context.
This commit is contained in:
@@ -8,6 +8,7 @@ class Context(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.functions = dict()
|
self.functions = dict()
|
||||||
self.variables = dict() # Maps variable name to value
|
self.variables = dict() # Maps variable name to value
|
||||||
|
self.extractors = dict()
|
||||||
|
|
||||||
def import_requires(self, modules):
|
def import_requires(self, modules):
|
||||||
""" import required modules dynamicly
|
""" import required modules dynamicly
|
||||||
@@ -44,6 +45,20 @@ class Context(object):
|
|||||||
for var_name, var_value in variable_bind_map.items():
|
for var_name, var_value in variable_bind_map.items():
|
||||||
self.variables[var_name] = self.get_eval_value(var_value)
|
self.variables[var_name] = self.get_eval_value(var_value)
|
||||||
|
|
||||||
|
def bind_extractors(self, extract_binds):
|
||||||
|
""" Bind named extractors to value within the context.
|
||||||
|
value => parsed from requests.Response object
|
||||||
|
key => extractor name, can be used as variable in next testcases
|
||||||
|
@param (dict) extract_binds
|
||||||
|
{
|
||||||
|
"resp_status_code": "status_code",
|
||||||
|
"resp_headers": "headers",
|
||||||
|
"resp_headers_content_type": "headers.content-type",
|
||||||
|
"resp_content": "content"
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
self.extractors.update(extract_binds)
|
||||||
|
|
||||||
def get_eval_value(self, data):
|
def get_eval_value(self, data):
|
||||||
""" evaluate data recursively, each variable in data will be evaluated.
|
""" evaluate data recursively, each variable in data will be evaluated.
|
||||||
variables marker: ${variable}.
|
variables marker: ${variable}.
|
||||||
|
|||||||
@@ -41,6 +41,9 @@ class TestRunner(object):
|
|||||||
variable_binds = config_dict.get('variable_binds', [])
|
variable_binds = config_dict.get('variable_binds', [])
|
||||||
self.context.bind_variables(variable_binds)
|
self.context.bind_variables(variable_binds)
|
||||||
|
|
||||||
|
extract_binds = config_dict.get('extract_binds', {})
|
||||||
|
self.context.bind_extractors(extract_binds)
|
||||||
|
|
||||||
self.testcase_parser.update_variables_binds(self.context.variables)
|
self.testcase_parser.update_variables_binds(self.context.variables)
|
||||||
|
|
||||||
def parse_testcase(self, testcase):
|
def parse_testcase(self, testcase):
|
||||||
@@ -101,6 +104,7 @@ class TestRunner(object):
|
|||||||
raise exception.ParamsError("URL or METHOD missed!")
|
raise exception.ParamsError("URL or METHOD missed!")
|
||||||
|
|
||||||
resp_obj = self.client.request(url=url, method=method, **req_kwargs)
|
resp_obj = self.client.request(url=url, method=method, **req_kwargs)
|
||||||
|
response.extract_response(resp_obj, self.context)
|
||||||
diff_content = response.diff_response(resp_obj, testcase['response'])
|
diff_content = response.diff_response(resp_obj, testcase['response'])
|
||||||
success = False if diff_content else True
|
success = False if diff_content else True
|
||||||
return success, diff_content
|
return success, diff_content
|
||||||
|
|||||||
Reference in New Issue
Block a user