mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-16 18:37:35 +08:00
new feature: testcases can be layered, now we can define interface in api block individually
This commit is contained in:
69
ate/utils.py
69
ate/utils.py
@@ -11,7 +11,7 @@ import string
|
||||
import types
|
||||
|
||||
import yaml
|
||||
from ate import exception
|
||||
from ate import exception, testcase
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
try:
|
||||
@@ -24,6 +24,7 @@ except NameError:
|
||||
PYTHON_VERSION = 3
|
||||
|
||||
SECRET_KEY = "DebugTalk"
|
||||
api_overall_dict = {}
|
||||
|
||||
def gen_random_string(str_len):
|
||||
return ''.join(
|
||||
@@ -125,6 +126,7 @@ def load_testcases_by_path(path):
|
||||
"testcases": []
|
||||
}
|
||||
testcases_list = load_testcases(path)
|
||||
dir_path = os.path.dirname(os.path.abspath(path))
|
||||
|
||||
for item in testcases_list:
|
||||
for key in item:
|
||||
@@ -132,13 +134,76 @@ def load_testcases_by_path(path):
|
||||
testset["config"].update(item["config"])
|
||||
testset["name"] = item["config"].get("name", "")
|
||||
elif key == "test":
|
||||
testset["testcases"].append(item["test"])
|
||||
test_dict = item["test"]
|
||||
if "api" in test_dict:
|
||||
update_test_info(test_dict, dir_path)
|
||||
|
||||
testset["testcases"].append(test_dict)
|
||||
|
||||
return [testset] if testset["testcases"] else []
|
||||
|
||||
else:
|
||||
return []
|
||||
|
||||
def update_test_info(test_dict, dir_path):
|
||||
api_call = test_dict["api"]
|
||||
function_meta = testcase.parse_function(api_call)
|
||||
func_name = function_meta["func_name"]
|
||||
api_info = get_api_definition(func_name, dir_path)
|
||||
test_dict.update(api_info)
|
||||
|
||||
def get_api_definition(name, dir_path):
|
||||
""" get expected api from dir_path upward recursively
|
||||
@param
|
||||
name: api name
|
||||
dir_path: start search dir path
|
||||
@return
|
||||
expected api info if found, otherwise raise ApiNotFound exception
|
||||
"""
|
||||
api_dir_dict = api_overall_dict.get(dir_path)
|
||||
if not api_dir_dict:
|
||||
api_dir_dict = load_api_definition(dir_path)
|
||||
api_overall_dict[dir_path] = api_dir_dict
|
||||
|
||||
api_info = api_dir_dict.get(name)
|
||||
if api_info:
|
||||
return api_info
|
||||
|
||||
parent_dir_path = os.path.dirname(dir_path)
|
||||
if dir_path == parent_dir_path:
|
||||
# system root path
|
||||
err_msg = "{} not found in recursive upward path!".format(name)
|
||||
raise exception.ApiNotFound(err_msg)
|
||||
|
||||
return get_api_definition(name, parent_dir_path)
|
||||
|
||||
def load_api_definition(dir_path):
|
||||
""" load all api definitions in specified dir path
|
||||
@param (str) dir_path
|
||||
@return (dict) all api definitions in dir_path merged in one dict
|
||||
"""
|
||||
api_files = load_folder_files(dir_path, file_type="api", recursive=False)
|
||||
|
||||
api_def_list = []
|
||||
for api_file in api_files:
|
||||
api_def_list.extend(load_testcases(api_file))
|
||||
|
||||
api_dir_dict = {}
|
||||
|
||||
for item in api_def_list:
|
||||
for key in item:
|
||||
if key == "api":
|
||||
api_def = item["api"].pop("def")
|
||||
function_meta = testcase.parse_function(api_def)
|
||||
func_name = function_meta["func_name"]
|
||||
|
||||
api_info = {}
|
||||
api_info["function_meta"] = function_meta
|
||||
api_info.update(item["api"])
|
||||
api_dir_dict[func_name] = api_info
|
||||
|
||||
return api_dir_dict
|
||||
|
||||
def query_json(json_content, query, delimiter='.'):
|
||||
""" Do an xpath-like query with json_content.
|
||||
@param (json_content) json_content
|
||||
|
||||
Reference in New Issue
Block a user