refactor testcase config data structure

This commit is contained in:
debugtalk
2018-11-15 21:49:05 +08:00
parent a67216cbdd
commit 5013861f74
5 changed files with 29 additions and 47 deletions

View File

@@ -151,13 +151,11 @@ class HttpRunner(object):
"name": "desc1",
"path": "testcase1_path",
"variables": [], # optional
"request": {} # optional
"refs": {
"functions": {},
"env": {},
"def-api": {},
"def-testcase": {}
}
"request": {}, # optional
"functions": {},
"env": {},
"def-api": {},
"def-testcase": {}
},
"teststeps": [
# 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):
""" 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
Args:
@@ -915,13 +915,11 @@ def load_tests(path, dot_env_path=None):
"name": "desc1",
"path": "testcase1_path",
"variables": [], # optional
"request": {} # optional
"refs": {
"functions": {},
"env": {},
"def-api": {},
"def-testcase": {}
}
"request": {}, # optional
"functions": {},
"env": {},
"def-api": {},
"def-testcase": {}
},
"teststeps": [
# teststep data structure
@@ -969,7 +967,7 @@ def load_tests(path, dot_env_path=None):
project_mapping = load_project_tests(path, dot_env_path)
testcase = _load_testcase(raw_testcase, project_mapping)
testcase["config"]["path"] = path
testcase["config"]["refs"] = project_mapping
testcase["config"].update(project_mapping)
testcases_list = [testcase]
except exceptions.FileFormatError:
testcases_list = []

View File

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

View File

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

View File

@@ -240,7 +240,7 @@ class TestModuleLoader(unittest.TestCase):
self.assertEqual(testcases[0]["config"]["name"], '123$var_a')
self.assertIn(
"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)
self.assertEqual(len(testcases_list), 1)
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
path = 'tests/data/demo_testcase_hardcode.yml'
testcases_list = loader.load_tests(path)
self.assertEqual(len(testcases_list), 1)
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)
path = [