mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-13 00:11:28 +08:00
split _load_teststeps from _load_testcase
This commit is contained in:
@@ -315,12 +315,70 @@ def get_module_item(module_mapping, item_type, item_name):
|
|||||||
## testcase loader
|
## testcase loader
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
|
def _load_teststeps(test_block, project_mapping):
|
||||||
|
""" load teststeps with api/testcase references
|
||||||
|
|
||||||
|
Args:
|
||||||
|
test_block (dict): test block content, maybe in 3 formats.
|
||||||
|
# api reference
|
||||||
|
{
|
||||||
|
"name": "add product to cart",
|
||||||
|
"api": "api_add_cart()",
|
||||||
|
"validate": []
|
||||||
|
}
|
||||||
|
# testcase reference
|
||||||
|
{
|
||||||
|
"name": "add product to cart",
|
||||||
|
"suite": "create_and_check()",
|
||||||
|
"validate": []
|
||||||
|
}
|
||||||
|
# define directly
|
||||||
|
{
|
||||||
|
"name": "checkout cart",
|
||||||
|
"request": {},
|
||||||
|
"validate": []
|
||||||
|
}
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list: loaded teststeps list
|
||||||
|
|
||||||
|
"""
|
||||||
|
def extend_api_definition(block):
|
||||||
|
ref_call = block["api"]
|
||||||
|
def_block = _get_block_by_name(ref_call, "def-api", project_mapping)
|
||||||
|
_extend_block(block, def_block)
|
||||||
|
|
||||||
|
teststeps = []
|
||||||
|
|
||||||
|
# reference api
|
||||||
|
if "api" in test_block:
|
||||||
|
extend_api_definition(test_block)
|
||||||
|
teststeps.append(test_block)
|
||||||
|
|
||||||
|
# reference testcase
|
||||||
|
elif "suite" in test_block: # TODO: replace suite with testcase
|
||||||
|
ref_call = test_block["suite"]
|
||||||
|
block = _get_block_by_name(ref_call, "def-testcase", project_mapping)
|
||||||
|
# TODO: bugfix lost block config variables
|
||||||
|
for teststep in block["teststeps"]:
|
||||||
|
if "api" in teststep:
|
||||||
|
extend_api_definition(teststep)
|
||||||
|
teststeps.append(teststep)
|
||||||
|
|
||||||
|
# define directly
|
||||||
|
else:
|
||||||
|
teststeps.append(test_block)
|
||||||
|
|
||||||
|
return teststeps
|
||||||
|
|
||||||
|
|
||||||
def _load_testcase(raw_testcase, project_mapping):
|
def _load_testcase(raw_testcase, project_mapping):
|
||||||
""" load testcase/testsuite with api/testcase references
|
""" load testcase/testsuite with api/testcase references
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
raw_testcase (list): raw testcase content loaded from JSON/YAML file:
|
raw_testcase (list): raw testcase content loaded from JSON/YAML file:
|
||||||
[
|
[
|
||||||
|
# config part
|
||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
"name": "",
|
"name": "",
|
||||||
@@ -328,26 +386,12 @@ def _load_testcase(raw_testcase, project_mapping):
|
|||||||
"request": {}
|
"request": {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
# teststeps part
|
||||||
{
|
{
|
||||||
"test": {
|
"test": {...}
|
||||||
"name": "add product to cart",
|
|
||||||
"api": "api_add_cart()",
|
|
||||||
"validate": []
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"test": {
|
"test": {...}
|
||||||
"name": "add product to cart",
|
|
||||||
"suite": "create_and_check()",
|
|
||||||
"validate": []
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"test": {
|
|
||||||
"name": "checkout cart",
|
|
||||||
"request": {},
|
|
||||||
"validate": []
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
project_mapping (dict): project_mapping
|
project_mapping (dict): project_mapping
|
||||||
@@ -378,30 +422,7 @@ def _load_testcase(raw_testcase, project_mapping):
|
|||||||
loaded_testcase["config"].update(test_block)
|
loaded_testcase["config"].update(test_block)
|
||||||
|
|
||||||
elif key == "test":
|
elif key == "test":
|
||||||
|
loaded_testcase["teststeps"].extend(_load_teststeps(test_block, project_mapping))
|
||||||
def extend_api_definition(block):
|
|
||||||
ref_call = block["api"]
|
|
||||||
def_block = _get_block_by_name(ref_call, "def-api", project_mapping)
|
|
||||||
_extend_block(block, def_block)
|
|
||||||
|
|
||||||
# reference api
|
|
||||||
if "api" in test_block:
|
|
||||||
extend_api_definition(test_block)
|
|
||||||
loaded_testcase["teststeps"].append(test_block)
|
|
||||||
|
|
||||||
# reference testcase
|
|
||||||
elif "suite" in test_block: # TODO: replace suite with testcase
|
|
||||||
ref_call = test_block["suite"]
|
|
||||||
block = _get_block_by_name(ref_call, "def-testcase", project_mapping)
|
|
||||||
# TODO: bugfix lost block config variables
|
|
||||||
for teststep in block["teststeps"]:
|
|
||||||
if "api" in teststep:
|
|
||||||
extend_api_definition(teststep)
|
|
||||||
loaded_testcase["teststeps"].append(teststep)
|
|
||||||
|
|
||||||
# define directly
|
|
||||||
else:
|
|
||||||
loaded_testcase["teststeps"].append(test_block)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logger.log_warning(
|
logger.log_warning(
|
||||||
@@ -917,7 +938,7 @@ def load_tests(path, dot_env_path=None):
|
|||||||
"teststeps": [
|
"teststeps": [
|
||||||
# teststep data structure
|
# teststep data structure
|
||||||
{
|
{
|
||||||
'name': 'test step desc2',
|
'name': 'test step desc1',
|
||||||
'variables': [], # optional
|
'variables': [], # optional
|
||||||
'extract': [], # optional
|
'extract': [], # optional
|
||||||
'validate': [],
|
'validate': [],
|
||||||
|
|||||||
@@ -283,7 +283,18 @@ class TestSuiteLoader(unittest.TestCase):
|
|||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
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_teststeps(self):
|
||||||
|
test_block = {
|
||||||
|
"name": "setup and reset all.",
|
||||||
|
"suite": "setup_and_reset($device_sn)",
|
||||||
|
"output": ["token", "device_sn"]
|
||||||
|
}
|
||||||
|
teststeps = loader._load_teststeps(test_block, self.project_mapping)
|
||||||
|
self.assertEqual(len(teststeps), 2)
|
||||||
|
self.assertEqual(teststeps[0]["name"], "get token")
|
||||||
|
self.assertEqual(teststeps[1]["name"], "reset all users")
|
||||||
|
|
||||||
|
def test_load_testcase(self):
|
||||||
raw_testcase = loader.load_file("tests/testcases/smoketest.yml")
|
raw_testcase = loader.load_file("tests/testcases/smoketest.yml")
|
||||||
testcase = loader._load_testcase(raw_testcase, self.project_mapping)
|
testcase = loader._load_testcase(raw_testcase, self.project_mapping)
|
||||||
self.assertEqual(testcase["config"]["name"], "smoketest")
|
self.assertEqual(testcase["config"]["name"], "smoketest")
|
||||||
|
|||||||
Reference in New Issue
Block a user