refactor testcase config data structure

This commit is contained in:
httprunner
2018-11-15 21:49:05 +08:00
parent efc2520e81
commit 4142867fbd
5 changed files with 29 additions and 47 deletions

View File

@@ -151,13 +151,11 @@ class HttpRunner(object):
"name": "desc1", "name": "desc1",
"path": "testcase1_path", "path": "testcase1_path",
"variables": [], # optional "variables": [], # optional
"request": {} # optional "request": {}, # optional
"refs": { "functions": {},
"functions": {}, "env": {},
"env": {}, "def-api": {},
"def-api": {}, "def-testcase": {}
"def-testcase": {}
}
}, },
"teststeps": [ "teststeps": [
# teststep data structure # teststep data structure

View File

@@ -867,7 +867,7 @@ def load_debugtalk_py(start_path):
def load_project_tests(test_path, dot_env_path=None): def load_project_tests(test_path, dot_env_path=None):
""" load api, testcases, .env, builtin module and debugtalk.py. """ load api, testcases, .env, debugtalk.py functions.
api/testcases folder is relative to project_working_directory api/testcases folder is relative to project_working_directory
Args: Args:
@@ -915,13 +915,11 @@ def load_tests(path, dot_env_path=None):
"name": "desc1", "name": "desc1",
"path": "testcase1_path", "path": "testcase1_path",
"variables": [], # optional "variables": [], # optional
"request": {} # optional "request": {}, # optional
"refs": { "functions": {},
"functions": {}, "env": {},
"env": {}, "def-api": {},
"def-api": {}, "def-testcase": {}
"def-testcase": {}
}
}, },
"teststeps": [ "teststeps": [
# teststep data structure # teststep data structure
@@ -969,7 +967,7 @@ def load_tests(path, dot_env_path=None):
project_mapping = load_project_tests(path, dot_env_path) project_mapping = load_project_tests(path, dot_env_path)
testcase = _load_testcase(raw_testcase, project_mapping) testcase = _load_testcase(raw_testcase, project_mapping)
testcase["config"]["path"] = path testcase["config"]["path"] = path
testcase["config"]["refs"] = project_mapping testcase["config"].update(project_mapping)
testcases_list = [testcase] testcases_list = [testcase]
except exceptions.FileFormatError: except exceptions.FileFormatError:
testcases_list = [] testcases_list = []

View File

@@ -547,15 +547,10 @@ def parse_tests(testcases, variables_mapping=None):
"path": "testcase1_path", "path": "testcase1_path",
"variables": {}, # optional "variables": {}, # optional
"request": {} # optional "request": {} # optional
"refs": { "functions": {},
"debugtalk": { "env": {},
"variables": {}, "def-api": {},
"functions": {} "def-testcase": {}
},
"env": {},
"def-api": {},
"def-testcase": {}
}
}, },
"teststeps": [ "teststeps": [
# teststep data structure # teststep data structure
@@ -583,17 +578,9 @@ def parse_tests(testcases, variables_mapping=None):
for testcase in testcases: for testcase in testcases:
testcase_config = testcase.setdefault("config", {}) testcase_config = testcase.setdefault("config", {})
project_mapping = testcase_config.pop( functions = testcase_config.get("functions", {})
"refs",
{
"functions": {},
"env": {},
"def-api": {},
"def-testcase": {}
}
)
env_mapping = project_mapping["env"] env_mapping = testcase_config.get("env", {})
# set OS environment variables # set OS environment variables
utils.set_os_environ(env_mapping) utils.set_os_environ(env_mapping)
@@ -602,7 +589,7 @@ def parse_tests(testcases, variables_mapping=None):
cartesian_product_parameters_list = parse_parameters( cartesian_product_parameters_list = parse_parameters(
config_parameters, config_parameters,
testcase_config.get("variables", {}), testcase_config.get("variables", {}),
project_mapping["functions"] functions
) or [{}] ) or [{}]
for parameter_mapping in cartesian_product_parameters_list: for parameter_mapping in cartesian_product_parameters_list:
@@ -627,7 +614,7 @@ def parse_tests(testcases, variables_mapping=None):
parsed_value = parse_data( parsed_value = parse_data(
value, value,
config_variables, config_variables,
project_mapping["functions"] functions
) )
config_variables[key] = parsed_value config_variables[key] = parsed_value
@@ -637,18 +624,18 @@ def parse_tests(testcases, variables_mapping=None):
testcase_dict["config"]["name"] = parse_data( testcase_dict["config"]["name"] = parse_data(
testcase_dict["config"].get("name", ""), testcase_dict["config"].get("name", ""),
config_variables, config_variables,
project_mapping["functions"] functions
) )
# parse config request # parse config request
testcase_dict["config"]["request"] = parse_data( testcase_dict["config"]["request"] = parse_data(
testcase_dict["config"].get("request", {}), testcase_dict["config"].get("request", {}),
config_variables, config_variables,
project_mapping["functions"] functions
) )
# put loaded project functions to config # put loaded project functions to config
testcase_dict["config"]["functions"] = project_mapping["functions"] testcase_dict["config"]["functions"] = functions
parsed_testcases_list.append(testcase_dict) parsed_testcases_list.append(testcase_dict)
# unset OS environment variables # unset OS environment variables

View File

@@ -165,12 +165,11 @@ class TestHttpRunner(ApiServerUnittest):
self.assertLess(end_time - start_time, 60) self.assertLess(end_time - start_time, 60)
def test_run_httprunner_with_teardown_hooks_alter_response(self): def test_run_httprunner_with_teardown_hooks_alter_response(self):
config = {"name": "test teardown hooks"}
config.update(loader.load_project_tests("tests"))
testcases = [ testcases = [
{ {
"config": { "config": config,
"name": "test teardown hooks",
"refs": loader.load_project_tests("tests")
},
"teststeps": [ "teststeps": [
{ {
"name": "test teardown hooks", "name": "test teardown hooks",

View File

@@ -240,7 +240,7 @@ class TestModuleLoader(unittest.TestCase):
self.assertEqual(testcases[0]["config"]["name"], '123$var_a') self.assertEqual(testcases[0]["config"]["name"], '123$var_a')
self.assertIn( self.assertIn(
"sum_two", "sum_two",
testcases[0]["config"]["refs"]["functions"] testcases[0]["config"]["functions"]
) )
@@ -394,14 +394,14 @@ class TestSuiteLoader(unittest.TestCase):
testcases_list = loader.load_tests(path) testcases_list = loader.load_tests(path)
self.assertEqual(len(testcases_list), 1) self.assertEqual(len(testcases_list), 1)
self.assertEqual(len(testcases_list[0]["teststeps"]), 3) self.assertEqual(len(testcases_list[0]["teststeps"]), 3)
self.assertIn("get_sign", testcases_list[0]["config"]["refs"]["functions"]) self.assertIn("get_sign", testcases_list[0]["config"]["functions"])
# relative file path # relative file path
path = 'tests/data/demo_testcase_hardcode.yml' path = 'tests/data/demo_testcase_hardcode.yml'
testcases_list = loader.load_tests(path) testcases_list = loader.load_tests(path)
self.assertEqual(len(testcases_list), 1) self.assertEqual(len(testcases_list), 1)
self.assertEqual(len(testcases_list[0]["teststeps"]), 3) self.assertEqual(len(testcases_list[0]["teststeps"]), 3)
self.assertIn("get_sign", testcases_list[0]["config"]["refs"]["functions"]) self.assertIn("get_sign", testcases_list[0]["config"]["functions"])
# list/set container with file(s) # list/set container with file(s)
path = [ path = [