mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-11 07:22:33 +08:00
add load_api_folder
This commit is contained in:
@@ -102,10 +102,14 @@ def load_file(file_path):
|
|||||||
|
|
||||||
|
|
||||||
def load_folder_files(folder_path, recursive=True):
|
def load_folder_files(folder_path, recursive=True):
|
||||||
""" load folder path, return all files in list format.
|
""" load folder path, return all files endswith yml/yaml/json in list.
|
||||||
@param
|
|
||||||
folder_path: specified folder path to load
|
Args:
|
||||||
recursive: if True, will load files recursively
|
folder_path (str): specified folder path to load
|
||||||
|
recursive (bool): load files recursively if True
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list: files endswith yml/yaml/json
|
||||||
"""
|
"""
|
||||||
if isinstance(folder_path, (list, set)):
|
if isinstance(folder_path, (list, set)):
|
||||||
files = []
|
files = []
|
||||||
@@ -735,6 +739,91 @@ def load_testcases(path):
|
|||||||
return testcases_list
|
return testcases_list
|
||||||
|
|
||||||
|
|
||||||
|
def load_folder_content(folder_path):
|
||||||
|
""" load api/testcases/testsuites definitions from folder.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
folder_path (str): api/testcases/testsuites files folder.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: api definition mapping.
|
||||||
|
|
||||||
|
{
|
||||||
|
"tests/api/basic.yml": [
|
||||||
|
{"api": {"def": "api_login", "request": {}, "validate": []}},
|
||||||
|
{"api": {"def": "api_logout", "request": {}, "validate": []}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
items_mapping = {}
|
||||||
|
|
||||||
|
for file_path in load_folder_files(folder_path):
|
||||||
|
items_mapping[file_path] = load_file(file_path)
|
||||||
|
|
||||||
|
return items_mapping
|
||||||
|
|
||||||
|
|
||||||
|
def load_api_folder(api_folder_path=None):
|
||||||
|
""" load api definitions from api folder.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
api_folder_path (str): api files folder.
|
||||||
|
|
||||||
|
api file should be in the following format:
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"api": {
|
||||||
|
"def": "api_login",
|
||||||
|
"request": {},
|
||||||
|
"validate": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"api": {
|
||||||
|
"def": "api_logout",
|
||||||
|
"request": {},
|
||||||
|
"validate": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: api definition mapping.
|
||||||
|
|
||||||
|
{
|
||||||
|
"api_login": {
|
||||||
|
"function_meta": {"func_name": "api_login", "args": [], "kwargs": {}}
|
||||||
|
"request": {}
|
||||||
|
},
|
||||||
|
"api_logout": {
|
||||||
|
"function_meta": {"func_name": "api_logout", "args": [], "kwargs": {}}
|
||||||
|
"request": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
api_definition_mapping = {}
|
||||||
|
|
||||||
|
api_folder_path = api_folder_path or os.path.join(os.getcwd(), "api")
|
||||||
|
api_items_mapping = load_folder_content(api_folder_path)
|
||||||
|
|
||||||
|
for api_file_path, api_items in api_items_mapping.items():
|
||||||
|
# TODO: add JSON schema validation
|
||||||
|
for api_item in api_items:
|
||||||
|
key, api_dict = api_item.popitem()
|
||||||
|
|
||||||
|
api_def = api_dict.pop("def")
|
||||||
|
function_meta = parser.parse_function(api_def)
|
||||||
|
func_name = function_meta["func_name"]
|
||||||
|
|
||||||
|
if func_name in api_definition_mapping:
|
||||||
|
logger.log_warning("API definition duplicated: {}".format(func_name))
|
||||||
|
|
||||||
|
api_dict["function_meta"] = function_meta
|
||||||
|
api_definition_mapping[func_name] = api_dict
|
||||||
|
|
||||||
|
return api_definition_mapping
|
||||||
|
|
||||||
|
|
||||||
def load(path):
|
def load(path):
|
||||||
""" main interface for loading testcases
|
""" main interface for loading testcases
|
||||||
|
|
||||||
|
|||||||
@@ -456,3 +456,17 @@ class TestSuiteLoader(unittest.TestCase):
|
|||||||
self.assertIn("request", testsets_list[0]["testcases"][0])
|
self.assertIn("request", testsets_list[0]["testcases"][0])
|
||||||
self.assertIn("url", testsets_list[0]["testcases"][0]["request"])
|
self.assertIn("url", testsets_list[0]["testcases"][0]["request"])
|
||||||
self.assertIn("validate", testsets_list[0]["testcases"][0])
|
self.assertIn("validate", testsets_list[0]["testcases"][0])
|
||||||
|
|
||||||
|
def test_load_folder_content(self):
|
||||||
|
path = os.path.join(os.getcwd(), "tests", "api")
|
||||||
|
items_mapping = loader.load_folder_content(path)
|
||||||
|
file_path = os.path.join(os.getcwd(), "tests", "api", "basic.yml")
|
||||||
|
self.assertIn(file_path, items_mapping)
|
||||||
|
self.assertIsInstance(items_mapping[file_path], list)
|
||||||
|
|
||||||
|
def test_load_api_folder(self):
|
||||||
|
path = os.path.join(os.getcwd(), "tests", "api")
|
||||||
|
api_definition_mapping = loader.load_api_folder(path)
|
||||||
|
self.assertIn("get_token", api_definition_mapping)
|
||||||
|
self.assertIn("request", api_definition_mapping["get_token"])
|
||||||
|
self.assertIn("function_meta", api_definition_mapping["get_token"])
|
||||||
|
|||||||
Reference in New Issue
Block a user