From 949268fbfc187c1638ad8b94f489e844549467bf Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 19 Mar 2019 15:16:46 +0800 Subject: [PATCH] feat: implement json dump Python objects when save tests --- HISTORY.md | 6 ++++++ httprunner/__about__.py | 2 +- httprunner/utils.py | 30 +++++++++++++++++++----------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 590f10a2..926e2234 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,11 @@ # Release History +## 2.1.0 (2019-03-19) + +**Features** + +- implement json dump Python objects when save tests + ## 2.0.6 (2019-03-18) **Features** diff --git a/httprunner/__about__.py b/httprunner/__about__.py index 5f7b3a5e..01d4dc4f 100644 --- a/httprunner/__about__.py +++ b/httprunner/__about__.py @@ -1,7 +1,7 @@ __title__ = 'HttpRunner' __description__ = 'One-stop solution for HTTP(S) testing.' __url__ = 'https://github.com/HttpRunner/HttpRunner' -__version__ = '2.0.6' +__version__ = '2.1.0' __author__ = 'debugtalk' __author_email__ = 'mail@debugtalk.com' __license__ = 'Apache-2.0' diff --git a/httprunner/utils.py b/httprunner/utils.py index 04f4778b..12ec7717 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -649,6 +649,13 @@ def omit_long_data(body, omit_len=512): def dump_json_file(json_data, pwd_dir_path, dump_file_name): """ dump json data to file """ + class PythonObjectEncoder(json.JSONEncoder): + def default(self, obj): + try: + return super().default(self, obj) + except TypeError: + return str(obj) + logs_dir_path = os.path.join(pwd_dir_path, "logs") if not os.path.isdir(logs_dir_path): os.makedirs(logs_dir_path) @@ -663,7 +670,8 @@ def dump_json_file(json_data, pwd_dir_path, dump_file_name): json_data, indent=4, separators=(',', ':'), - ensure_ascii=False + ensure_ascii=False, + cls=PythonObjectEncoder )) ) else: @@ -672,14 +680,15 @@ def dump_json_file(json_data, pwd_dir_path, dump_file_name): outfile, indent=4, separators=(',', ':'), - ensure_ascii=False + ensure_ascii=False, + cls=PythonObjectEncoder ) msg = "dump file: {}".format(dump_file_path) logger.color_print(msg, "BLUE") - except TypeError: - msg = "Failed to dump json file: {}".format(dump_file_path) + except TypeError as ex: + msg = "Failed to dump json file: {}\nReason: {}".format(dump_file_path, ex) logger.color_print(msg, "RED") @@ -711,14 +720,13 @@ def dump_tests(tests_mapping, tag_name): } for key in project_mapping: - if key != "functions": + if key == "functions" and project_mapping["functions"]: + tests_to_dump["project_mapping"]["debugtalk.py"] = { + "path": os.path.join(pwd_dir_path, "debugtalk.py"), + "functions": project_mapping["functions"] + } + else: tests_to_dump["project_mapping"][key] = project_mapping[key] - continue - - # remove functions in order to dump - if project_mapping["functions"]: - debugtalk_py_path = os.path.join(pwd_dir_path, "debugtalk.py") - tests_to_dump["project_mapping"]["debugtalk.py"] = debugtalk_py_path if "api" in tests_mapping: tests_to_dump["api"] = tests_mapping["api"]