refactor load project_mapping

This commit is contained in:
debugtalk
2018-08-11 20:39:15 +08:00
parent fc473311ed
commit 391268b4c9
2 changed files with 45 additions and 22 deletions

View File

@@ -9,6 +9,18 @@ import yaml
from httprunner import exceptions, logger, parser, validator from httprunner import exceptions, logger, parser, validator
from httprunner.compat import OrderedDict from httprunner.compat import OrderedDict
project_mapping = {
"debugtalk": {},
"env": {},
"tests": {
"api": {},
"testcases": {}
}
}
""" dict: save project loaded api/testcases, environments and debugtalk.py module.
"""
############################################################################### ###############################################################################
## file loader ## file loader
############################################################################### ###############################################################################
@@ -185,6 +197,7 @@ def load_dot_env_file(path):
env_variables_mapping[variable.strip()] = value.strip() env_variables_mapping[variable.strip()] = value.strip()
project_mapping["env"] = env_variables_mapping
return env_variables_mapping return env_variables_mapping
@@ -311,7 +324,10 @@ def load_debugtalk_module(start_path=None):
} }
imported_module = importlib.import_module(module_name) imported_module = importlib.import_module(module_name)
return load_python_module(imported_module) loaded_module = load_python_module(imported_module)
project_mapping["debugtalk"] = loaded_module
return loaded_module
def get_module_item(module_mapping, item_type, item_name): def get_module_item(module_mapping, item_type, item_name):
@@ -348,10 +364,9 @@ def get_module_item(module_mapping, item_type, item_name):
############################################################################### ###############################################################################
## suite loader ## testcase loader
############################################################################### ###############################################################################
overall_def_dict = { overall_def_dict = {
"api": {}, "api": {},
"suite": {} "suite": {}
@@ -689,11 +704,16 @@ def _merge_extractor(def_extrators, current_extractors):
def load_testcases(path): def load_testcases(path):
""" load testcases from file path """ load testcases from file path
@param path: path could be in several type
- absolute/relative file path Args:
- absolute/relative folder path path (str): testcase file/foler path.
- list/set container with file(s) and/or folder(s) path could be in several types:
@return testcases list, each testcase is corresponding to a file - absolute/relative file path
- absolute/relative folder path
- list/set container with file(s) and/or folder(s)
Returns:
list: testcases list, each testcase is corresponding to a file
[ [
testcase_dict_1, testcase_dict_1,
testcase_dict_2 testcase_dict_2
@@ -823,6 +843,7 @@ def load_api_folder(api_folder_path=None):
api_dict["function_meta"] = function_meta api_dict["function_meta"] = function_meta
api_definition_mapping[func_name] = api_dict api_definition_mapping[func_name] = api_dict
project_mapping["tests"]["api"] = api_definition_mapping
return api_definition_mapping return api_definition_mapping
@@ -904,6 +925,7 @@ def load_test_folder(test_folder_path=None):
# key == "test": # key == "test":
testcase["tests"].append(block) testcase["tests"].append(block)
project_mapping["tests"]["testcases"] = test_definition_mapping
return test_definition_mapping return test_definition_mapping
@@ -919,13 +941,12 @@ def load_project_tests(folder_path=None):
""" """
folder_path = folder_path or os.getcwd() folder_path = folder_path or os.getcwd()
return {
"debugtalk": load_debugtalk_module(folder_path), load_debugtalk_module(folder_path)
"tests": { load_api_folder(os.path.join(folder_path, "api"))
"api": load_api_folder(os.path.join(folder_path, "api")), load_test_folder(os.path.join(folder_path, "suite"))
"testcases": load_test_folder(os.path.join(folder_path, "suite"))
} return project_mapping
}
def load(path): def load(path):

View File

@@ -210,7 +210,7 @@ def init_test_suites(path_or_testcases, mapping=None, http_client_session=None):
class HttpRunner(object): class HttpRunner(object):
def __init__(self, **kwargs): def __init__(self, **kwargs):
""" initialize HttpRunner """ initialize HttpRunner.
Args: Args:
kwargs (dict): key-value arguments used to initialize TextTestRunner. kwargs (dict): key-value arguments used to initialize TextTestRunner.
@@ -218,15 +218,17 @@ class HttpRunner(object):
resultclass (class): HtmlTestResult or TextTestResult resultclass (class): HtmlTestResult or TextTestResult
failfast (bool): False/True, stop the test run on the first error or failure. failfast (bool): False/True, stop the test run on the first error or failure.
dot_env_path (str): .env file path dot_env_path (str): .env file path.
Attributes:
project_mapping (dict): save project loaded api/testcases, environments and debugtalk.py module.
""" """
dot_env_path = kwargs.pop("dot_env_path", None) dot_env_path = kwargs.pop("dot_env_path", None)
env_mapping = loader.load_dot_env_file(dot_env_path) loader.load_dot_env_file(dot_env_path)
utils.set_os_environ(env_mapping) loader.load_project_tests("tests") # TODO: remove tests
# TODO: remove tests self.project_mapping = loader.project_mapping
self.project_mapping = loader.load_project_tests("tests") utils.set_os_environ(self.project_mapping["env"])
self.project_mapping["env"] = env_mapping
kwargs.setdefault("resultclass", HtmlTestResult) kwargs.setdefault("resultclass", HtmlTestResult)
self.runner = unittest.TextTestRunner(**kwargs) self.runner = unittest.TextTestRunner(**kwargs)