From 7fd243a7e89eb8a452e92d7dad530762fd8756fa Mon Sep 17 00:00:00 2001 From: debugtalk Date: Mon, 25 May 2020 18:52:59 +0800 Subject: [PATCH] change: adjust code --- httprunner/ext/make/__init__.py | 25 +++++++++++++++---------- httprunner/runner.py | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/httprunner/ext/make/__init__.py b/httprunner/ext/make/__init__.py index c0f045ff..71c7c52b 100644 --- a/httprunner/ext/make/__init__.py +++ b/httprunner/ext/make/__init__.py @@ -19,7 +19,8 @@ from httprunner.parser import parse_data """ make_files_cache_mapping: Dict[Text, List] = {} -__TMPL__ = """# NOTICE: Generated By HttpRunner. DO'NOT EDIT! +__TEMPLATE__ = jinja2.Template( + """# NOTICE: Generated By HttpRunner. DO'NOT EDIT! # FROM: {{ testcase_path }} from httprunner import HttpRunner, TConfig, TStep {% for import_str in imports_list %} @@ -40,6 +41,7 @@ if __name__ == "__main__": {{ class_name }}().test_start() """ +) def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]: @@ -75,7 +77,7 @@ def format_pytest_with_black(python_paths: List[Text]): logger.error(ex) -def make_testcase(testcase: Dict) -> Union[str, None]: +def make_testcase(testcase: Dict) -> Text: """convert valid testcase dict to pytest file path""" try: # validate testcase format @@ -87,8 +89,6 @@ def make_testcase(testcase: Dict) -> Union[str, None]: testcase_path = testcase["config"]["path"] logger.info(f"start to make testcase: {testcase_path}") - template = jinja2.Template(__TMPL__) - # convert abs path to relative if os.path.isabs(testcase_path): testcase_path = testcase_path[len(os.getcwd()) + 1 :] @@ -139,7 +139,7 @@ def make_testcase(testcase: Dict) -> Union[str, None]: "teststeps": teststeps, "imports_list": imports_list, } - content = template.render(data) + content = __TEMPLATE__.render(data) content = content.replace("'CLS_LB(", "").replace(")CLS_RB'", "") with open(testcase_python_path, "w", encoding="utf-8") as f: @@ -161,7 +161,6 @@ def make_testsuite(testsuite: Dict) -> List[Text]: config = testsuite["config"] testsuite_path = config["path"] project_meta = load_project_meta(testsuite_path) - project_working_directory = project_meta.PWD testsuite_variables = config.get("variables", {}) if isinstance(testsuite_variables, Text): @@ -173,7 +172,10 @@ def make_testsuite(testsuite: Dict) -> List[Text]: logger.info(f"start to make testsuite: {testsuite_path}") # create directory with testsuite file name, put its testcases under this directory - testsuite_dir = testsuite_path.replace(".", "_") + testsuite_dir = os.path.join( + os.path.dirname(testsuite_path), + os.path.basename(testsuite_path).replace(".", "_"), + ) os.makedirs(testsuite_dir, exist_ok=True) testcase_files = [] @@ -181,7 +183,11 @@ def make_testsuite(testsuite: Dict) -> List[Text]: for testcase in testsuite["testcases"]: # get referenced testcase content testcase_file = testcase["testcase"] - testcase_path = os.path.join(project_working_directory, testcase_file) + if os.path.isabs(testcase_file): + testcase_path = testcase_file + else: + testcase_path = os.path.join(project_meta.PWD, testcase_file) + testcase_dict = load_test_file(testcase_path) testcase_dict.setdefault("config", {}) testcase_dict["config"]["path"] = os.path.join( @@ -206,7 +212,7 @@ def make_testsuite(testsuite: Dict) -> List[Text]: return testcase_files -def __make(tests_path: Text) -> List: +def __make(tests_path: Text) -> List[Text]: global make_files_cache_mapping if tests_path in make_files_cache_mapping: return make_files_cache_mapping[tests_path] @@ -263,7 +269,6 @@ def __make(tests_path: Text) -> List: def main_make(tests_paths: List[Text]) -> List: - for tests_path in tests_paths: __make(tests_path) diff --git a/httprunner/runner.py b/httprunner/runner.py index 5a890f82..35a7db93 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -167,7 +167,7 @@ class HttpRunner(object): else: raise exceptions.ParamsError( - f"Invalid teststep referenced testcase: {step}" + f"Invalid teststep referenced testcase: {step.dict()}" ) step_data.data = case_result.get_step_datas() # list of step data