diff --git a/httprunner/__about__.py b/httprunner/__about__.py index 2b036597..44b5f9db 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__ = '1.3.0' +__version__ = '1.3.1' __author__ = 'debugtalk' __author_email__ = 'mail@debugtalk.com' __license__ = 'MIT' diff --git a/httprunner/cli.py b/httprunner/cli.py index 01c74076..83462dcc 100644 --- a/httprunner/cli.py +++ b/httprunner/cli.py @@ -8,7 +8,7 @@ from httprunner import logger from httprunner.__about__ import __version__ from httprunner.task import HttpRunner from httprunner.utils import (create_scaffold, load_dot_env_file, print_output, - string_type, validate_json_file) + string_type, validate_json_file, prettify_json_file) def main_hrun(): @@ -43,6 +43,9 @@ def main_hrun(): parser.add_argument( '--validate', nargs='*', help="Validate JSON testset format.") + parser.add_argument( + '--prettify', nargs='*', + help="Prettify JSON testset format.") args = parser.parse_args() logger.setup_logger(args.log_level) @@ -54,6 +57,9 @@ def main_hrun(): if args.validate: validate_json_file(args.validate) exit(0) + if args.prettify: + prettify_json_file(args.prettify) + exit(0) dot_env_path = args.dot_env_path or os.path.join(os.getcwd(), ".env") if dot_env_path: diff --git a/httprunner/utils.py b/httprunner/utils.py index 5cf64e6c..c8006317 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -431,12 +431,38 @@ def validate_json_file(file_list): logger.log_warning("Only JSON file format can be validated.") continue - logger.color_print("start to validate JSON file: {}".format(json_file), "GREEN") + logger.color_print("Start to validate JSON file: {}".format(json_file), "GREEN") - with open(json_file) as stream: + with io.open(json_file) as stream: try: json.load(stream) except ValueError as e: raise SystemExit(e) print("OK") + +def prettify_json_file(file_list): + """ prettify JSON testset format + """ + for json_file in set(file_list): + if not json_file.endswith(".json"): + logger.log_warning("Only JSON file format can be prettified.") + continue + + logger.color_print("Start to prettify JSON file: {}".format(json_file), "GREEN") + + dir_path = os.path.dirname(json_file) + file_name, file_suffix = os.path.splitext(os.path.basename(json_file)) + outfile = os.path.join(dir_path, "{}.pretty.json".format(file_name)) + + with io.open(json_file, 'r', encoding='utf-8') as stream: + try: + obj = json.load(stream) + except ValueError as e: + raise SystemExit(e) + + with io.open(outfile, 'w', encoding='utf-8') as out: + json.dump(obj, out, indent=4, separators=(',', ': ')) + out.write('\n') + + print("success: {}".format(outfile))