mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-30 21:09:36 +08:00
refactor context:
1, testset and testcase are in different context level; 2, testset context will be initialized when a file is loaded, and testcase level context initializes when each testcase starts; 3, testcase context should inherit from testset context configs, and the testcase context has high priority.
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import copy
|
||||
import requests
|
||||
|
||||
from ate import exception, response
|
||||
from ate.context import Context
|
||||
from ate.testcase import parse_template
|
||||
|
||||
|
||||
class TestRunner(object):
|
||||
@@ -11,9 +9,8 @@ class TestRunner(object):
|
||||
def __init__(self):
|
||||
self.client = requests.Session()
|
||||
self.context = Context()
|
||||
self.testset_req_overall_configs = {}
|
||||
|
||||
def update_context(self, config_dict, level="testcase"):
|
||||
def init_context(self, config_dict, level):
|
||||
""" create/update context variables binds
|
||||
@param (dict) config_dict
|
||||
{
|
||||
@@ -27,28 +24,28 @@ class TestRunner(object):
|
||||
"lambda *str_args: hashlib.md5(''.join(str_args).\
|
||||
encode('utf-8')).hexdigest()"
|
||||
},
|
||||
"import_module_functions": ["test.data.custom_functions"],
|
||||
"variable_binds": [
|
||||
{"TOKEN": "debugtalk"},
|
||||
{"random": {"func": "gen_random_string", "args": [5]}},
|
||||
]
|
||||
}
|
||||
@param (str) context level, testcase or testset
|
||||
only when level is testset, shall we update testset_req_overall_configs
|
||||
"""
|
||||
requires = config_dict.get('requires', [])
|
||||
self.context.import_requires(requires)
|
||||
|
||||
function_binds = config_dict.get('function_binds', {})
|
||||
self.context.bind_functions(function_binds)
|
||||
self.context.bind_functions(function_binds, level)
|
||||
|
||||
module_functions = config_dict.get('import_module_functions', [])
|
||||
self.context.import_module_functions(module_functions)
|
||||
self.context.import_module_functions(module_functions, level)
|
||||
|
||||
variable_binds = config_dict.get('variable_binds', [])
|
||||
self.context.bind_variables(variable_binds)
|
||||
self.context.register_variables_config(variable_binds, level)
|
||||
|
||||
if level == "testset":
|
||||
self.testset_req_overall_configs = config_dict.get('request', {})
|
||||
request_config = config_dict.get('request', {})
|
||||
self.context.register_request(request_config, level)
|
||||
|
||||
def run_test(self, testcase):
|
||||
""" run single testcase.
|
||||
@@ -68,21 +65,14 @@ class TestRunner(object):
|
||||
},
|
||||
"body": '{"name": "user", "password": "123456"}'
|
||||
},
|
||||
"extract_binds": {},
|
||||
"validators": []
|
||||
"extract_binds": {}, # optional
|
||||
"validators": [] # optional
|
||||
}
|
||||
@return (tuple) test result of single testcase
|
||||
(success, diff_content_list)
|
||||
"""
|
||||
self.update_context(testcase)
|
||||
|
||||
# each testcase shall inherit from testset request configs,
|
||||
# but can not override testset configs,
|
||||
# that's why we use copy.deepcopy here.
|
||||
testcase_request = copy.deepcopy(self.testset_req_overall_configs)
|
||||
testcase_request.update(testcase["request"])
|
||||
|
||||
parsed_request = parse_template(testcase_request, self.context.variables)
|
||||
self.init_context(testcase, level="testcase")
|
||||
parsed_request = self.context.get_parsed_request()
|
||||
try:
|
||||
url = parsed_request.pop('url')
|
||||
method = parsed_request.pop('method')
|
||||
@@ -100,10 +90,11 @@ class TestRunner(object):
|
||||
|
||||
extract_binds = testcase.get("extract_binds", {})
|
||||
extracted_variables_mapping = resp_obj.extract_response(extract_binds)
|
||||
self.context.update_variables(extracted_variables_mapping)
|
||||
self.context.bind_extracted_variables(extracted_variables_mapping)
|
||||
|
||||
validators = testcase.get("validators", [])
|
||||
diff_content_list = resp_obj.validate(validators, self.context.variables)
|
||||
diff_content_list = resp_obj.validate(
|
||||
validators, self.context.get_testcase_variables_mapping())
|
||||
|
||||
return resp_obj.success, diff_content_list
|
||||
|
||||
@@ -122,10 +113,10 @@ class TestRunner(object):
|
||||
"testcases": [
|
||||
{
|
||||
"name": "testcase description",
|
||||
"variable_binds": {}, # override
|
||||
"variable_binds": {}, # optional, override
|
||||
"request": {},
|
||||
"extract_binds": {},
|
||||
"validators": {}
|
||||
"extract_binds": {}, # optional
|
||||
"validators": {} # optional
|
||||
},
|
||||
testcase12
|
||||
]
|
||||
@@ -139,7 +130,7 @@ class TestRunner(object):
|
||||
results = []
|
||||
|
||||
config_dict = testset.get("config", {})
|
||||
self.update_context(config_dict)
|
||||
self.init_context(config_dict, level="testset")
|
||||
testcases = testset.get("testcases", [])
|
||||
for testcase in testcases:
|
||||
result = self.run_test(testcase)
|
||||
|
||||
Reference in New Issue
Block a user