bind_extractors: Bind named extractors to value within the context.

This commit is contained in:
httprunner
2017-06-28 21:38:43 +08:00
parent fc9218c70c
commit 6438fe653a
2 changed files with 19 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ class Context(object):
def __init__(self):
self.functions = dict()
self.variables = dict() # Maps variable name to value
self.extractors = dict()
def import_requires(self, modules):
""" import required modules dynamicly
@@ -44,6 +45,20 @@ class Context(object):
for var_name, var_value in variable_bind_map.items():
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):
""" evaluate data recursively, each variable in data will be evaluated.
variables marker: ${variable}.

View File

@@ -41,6 +41,9 @@ class TestRunner(object):
variable_binds = config_dict.get('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)
def parse_testcase(self, testcase):
@@ -101,6 +104,7 @@ class TestRunner(object):
raise exception.ParamsError("URL or METHOD missed!")
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'])
success = False if diff_content else True
return success, diff_content