mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-06 16:07:45 +08:00
refactor _load_test_file
This commit is contained in:
+15
-14
@@ -315,11 +315,11 @@ def get_module_item(module_mapping, item_type, item_name):
|
|||||||
## testcase loader
|
## testcase loader
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
def _load_test_file(file_path, project_mapping):
|
def _load_testcase(raw_testcase, project_mapping):
|
||||||
""" load testcase file or testsuite file
|
""" load testcase/testsuite with api/testcase references
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
file_path (str): absolute valid file path. file_path should be in the following format:
|
raw_testcase (list): raw testcase content loaded from JSON/YAML file:
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
@@ -353,29 +353,29 @@ def _load_test_file(file_path, project_mapping):
|
|||||||
project_mapping (dict): project_mapping
|
project_mapping (dict): project_mapping
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: testcase dict
|
dict: loaded testcase content
|
||||||
{
|
{
|
||||||
"config": {},
|
"config": {},
|
||||||
"teststeps": [teststep11, teststep12]
|
"teststeps": [teststep11, teststep12]
|
||||||
}
|
}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
testcase = {
|
loaded_testcase = {
|
||||||
"config": {},
|
"config": {},
|
||||||
"teststeps": []
|
"teststeps": []
|
||||||
}
|
}
|
||||||
|
|
||||||
for item in load_file(file_path):
|
for item in raw_testcase:
|
||||||
# TODO: add json schema validation
|
# TODO: add json schema validation
|
||||||
if not isinstance(item, dict) or len(item) != 1:
|
if not isinstance(item, dict) or len(item) != 1:
|
||||||
raise exceptions.FileFormatError("Testcase format error: {}".format(file_path))
|
raise exceptions.FileFormatError("Testcase format error: {}".format(item))
|
||||||
|
|
||||||
key, test_block = item.popitem()
|
key, test_block = item.popitem()
|
||||||
if not isinstance(test_block, dict):
|
if not isinstance(test_block, dict):
|
||||||
raise exceptions.FileFormatError("Testcase format error: {}".format(file_path))
|
raise exceptions.FileFormatError("Testcase format error: {}".format(item))
|
||||||
|
|
||||||
if key == "config":
|
if key == "config":
|
||||||
testcase["config"].update(test_block)
|
loaded_testcase["config"].update(test_block)
|
||||||
|
|
||||||
elif key == "test":
|
elif key == "test":
|
||||||
|
|
||||||
@@ -387,7 +387,7 @@ def _load_test_file(file_path, project_mapping):
|
|||||||
# reference api
|
# reference api
|
||||||
if "api" in test_block:
|
if "api" in test_block:
|
||||||
extend_api_definition(test_block)
|
extend_api_definition(test_block)
|
||||||
testcase["teststeps"].append(test_block)
|
loaded_testcase["teststeps"].append(test_block)
|
||||||
|
|
||||||
# reference testcase
|
# reference testcase
|
||||||
elif "suite" in test_block: # TODO: replace suite with testcase
|
elif "suite" in test_block: # TODO: replace suite with testcase
|
||||||
@@ -397,18 +397,18 @@ def _load_test_file(file_path, project_mapping):
|
|||||||
for teststep in block["teststeps"]:
|
for teststep in block["teststeps"]:
|
||||||
if "api" in teststep:
|
if "api" in teststep:
|
||||||
extend_api_definition(teststep)
|
extend_api_definition(teststep)
|
||||||
testcase["teststeps"].append(teststep)
|
loaded_testcase["teststeps"].append(teststep)
|
||||||
|
|
||||||
# define directly
|
# define directly
|
||||||
else:
|
else:
|
||||||
testcase["teststeps"].append(test_block)
|
loaded_testcase["teststeps"].append(test_block)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logger.log_warning(
|
logger.log_warning(
|
||||||
"unexpected block key: {}. block key should only be 'config' or 'test'.".format(key)
|
"unexpected block key: {}. block key should only be 'config' or 'test'.".format(key)
|
||||||
)
|
)
|
||||||
|
|
||||||
return testcase
|
return loaded_testcase
|
||||||
|
|
||||||
|
|
||||||
def _get_block_by_name(ref_call, ref_type, project_mapping):
|
def _get_block_by_name(ref_call, ref_type, project_mapping):
|
||||||
@@ -956,8 +956,9 @@ def load_tests(path, dot_env_path=None):
|
|||||||
|
|
||||||
elif os.path.isfile(path):
|
elif os.path.isfile(path):
|
||||||
try:
|
try:
|
||||||
|
raw_testcase = load_file(path)
|
||||||
project_mapping = load_project_tests(path, dot_env_path)
|
project_mapping = load_project_tests(path, dot_env_path)
|
||||||
testcase = _load_test_file(path, project_mapping)
|
testcase = _load_testcase(raw_testcase, project_mapping)
|
||||||
testcase["config"]["path"] = path
|
testcase["config"]["path"] = path
|
||||||
testcase["config"]["refs"] = project_mapping
|
testcase["config"]["refs"] = project_mapping
|
||||||
testcases_list = [testcase]
|
testcases_list = [testcase]
|
||||||
|
|||||||
@@ -284,7 +284,8 @@ class TestSuiteLoader(unittest.TestCase):
|
|||||||
cls.project_mapping = loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
|
cls.project_mapping = loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
|
||||||
|
|
||||||
def test_load_test_file_testcase(self):
|
def test_load_test_file_testcase(self):
|
||||||
testcase = loader._load_test_file("tests/testcases/smoketest.yml", self.project_mapping)
|
raw_testcase = loader.load_file("tests/testcases/smoketest.yml")
|
||||||
|
testcase = loader._load_testcase(raw_testcase, self.project_mapping)
|
||||||
self.assertEqual(testcase["config"]["name"], "smoketest")
|
self.assertEqual(testcase["config"]["name"], "smoketest")
|
||||||
self.assertIn("device_sn", testcase["config"]["variables"][0])
|
self.assertIn("device_sn", testcase["config"]["variables"][0])
|
||||||
self.assertEqual(len(testcase["teststeps"]), 8)
|
self.assertEqual(len(testcase["teststeps"]), 8)
|
||||||
|
|||||||
Reference in New Issue
Block a user