From 57040ad4aef6d64a9638146032628f8677011a1b Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 10 Mar 2020 17:58:21 +0800 Subject: [PATCH] change: generate logs folder in current working directory --- docs/CHANGELOG.md | 2 +- httprunner/loader/buildup.py | 2 +- httprunner/utils.py | 11 +++++------ httprunner/utils_test.py | 14 +++++--------- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index c72daf87..83459198 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -8,7 +8,7 @@ - remove support for Python 2.7 - replace string format with f-string - remove dependency colorama and colorlog -- generate reports folder in current working directory +- generate reports/logs folder in current working directory ## 2.5.7 (2020-02-21) diff --git a/httprunner/loader/buildup.py b/httprunner/loader/buildup.py index df40cfe9..12f2bc7f 100644 --- a/httprunner/loader/buildup.py +++ b/httprunner/loader/buildup.py @@ -419,7 +419,7 @@ def load_project_data(test_path, dot_env_path=None): # locate PWD and load debugtalk.py functions project_mapping["PWD"] = project_working_directory project_mapping["functions"] = debugtalk_functions - project_mapping["test_path"] = os.path.abspath(test_path) + project_mapping["test_path"] = os.path.abspath(test_path)[len(project_working_directory)+1:] return project_mapping diff --git a/httprunner/utils.py b/httprunner/utils.py index aa243af9..bfda61c1 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -596,24 +596,23 @@ def dump_json_file(json_data, json_file_abs_path): def prepare_dump_json_file_abs_path(project_mapping, tag_name): """ prepare dump json file absolute path. """ - pwd_dir_path = project_mapping.get("PWD") or os.getcwd() + current_working_dir = os.getcwd() test_path = project_mapping.get("test_path") if not test_path: # running passed in testcase/testsuite data structure dump_file_name = f"tests_mapping.{tag_name}.json" - dumped_json_file_abs_path = os.path.join(pwd_dir_path, "logs", dump_file_name) + dumped_json_file_abs_path = os.path.join(current_working_dir, "logs", dump_file_name) return dumped_json_file_abs_path # both test_path and pwd_dir_path are absolute path - logs_dir_path = os.path.join(pwd_dir_path, "logs") - test_path_relative_path = test_path[len(pwd_dir_path)+1:] + logs_dir_path = os.path.join(current_working_dir, "logs") if os.path.isdir(test_path): - file_foder_path = os.path.join(logs_dir_path, test_path_relative_path) + file_foder_path = os.path.join(logs_dir_path, test_path) dump_file_name = f"all.{tag_name}.json" else: - file_relative_folder_path, test_file = os.path.split(test_path_relative_path) + file_relative_folder_path, test_file = os.path.split(test_path) file_foder_path = os.path.join(logs_dir_path, file_relative_folder_path) test_file_name, _file_suffix = os.path.splitext(test_file) dump_file_name = f"{test_file_name}.{tag_name}.json" diff --git a/httprunner/utils_test.py b/httprunner/utils_test.py index 28d1c751..8f8afc4a 100644 --- a/httprunner/utils_test.py +++ b/httprunner/utils_test.py @@ -278,26 +278,22 @@ class TestUtils(unittest.TestCase): def test_prepare_dump_json_file_path_for_folder(self): # hrun tests/httpbin/a.b.c/ --save-tests - project_working_directory = os.path.join(os.getcwd(), "tests") project_mapping = { - "PWD": project_working_directory, - "test_path": os.path.join(os.getcwd(), "tests", "httpbin", "a.b.c") + "test_path": os.path.join("tests", "httpbin", "a.b.c") } self.assertEqual( utils.prepare_dump_json_file_abs_path(project_mapping, "loaded"), - os.path.join(project_working_directory, "logs", "httpbin/a.b.c/all.loaded.json") + os.path.join(os.getcwd(), "logs", "tests/httpbin/a.b.c/all.loaded.json") ) def test_prepare_dump_json_file_path_for_file(self): # hrun tests/httpbin/a.b.c/rpc.yml --save-tests - project_working_directory = os.path.join(os.getcwd(), "tests") project_mapping = { - "PWD": project_working_directory, - "test_path": os.path.join(os.getcwd(), "tests", "httpbin", "a.b.c", "rpc.yml") + "test_path": os.path.join("tests", "httpbin", "a.b.c", "rpc.yml") } self.assertEqual( utils.prepare_dump_json_file_abs_path(project_mapping, "loaded"), - os.path.join(project_working_directory, "logs", "httpbin/a.b.c/rpc.loaded.json") + os.path.join(os.getcwd(), "logs", "tests/httpbin/a.b.c/rpc.loaded.json") ) def test_prepare_dump_json_file_path_for_passed_testcase(self): @@ -307,5 +303,5 @@ class TestUtils(unittest.TestCase): } self.assertEqual( utils.prepare_dump_json_file_abs_path(project_mapping, "loaded"), - os.path.join(project_working_directory, "logs", "tests_mapping.loaded.json") + os.path.join(os.getcwd(), "logs", "tests_mapping.loaded.json") )