feat: implement json dump Python objects when save tests

This commit is contained in:
debugtalk
2019-03-19 15:16:46 +08:00
parent f60681d668
commit 949268fbfc
3 changed files with 26 additions and 12 deletions

View File

@@ -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**

View File

@@ -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'

View File

@@ -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"]