From accc06733c03427dd3776f387a5ba6d786a3380f Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 1 Nov 2019 19:34:26 +0800 Subject: [PATCH] change: --report-file, specify report file path, this has higher priority than specifying report dir. --- CHANGELOG.md | 7 +++++++ httprunner/__init__.py | 2 +- httprunner/api.py | 4 ++++ httprunner/cli.py | 6 ++++-- httprunner/report.py | 15 +++++++++------ 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8f6e0d0..ff31a5ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Release History +## 2.3.2 (2019-11-01) + +**Changed** + +- make render_html_report separate with HttpRunner().run_tests() +- `--report-file`: specify report file path, this has higher priority than specifying report dir. + ## 2.3.1 (2019-10-28) **Fixed** diff --git a/httprunner/__init__.py b/httprunner/__init__.py index b6acc5ef..5b67afa6 100644 --- a/httprunner/__init__.py +++ b/httprunner/__init__.py @@ -1,4 +1,4 @@ -__version__ = "2.3.1" +__version__ = "2.3.2" __description__ = "One-stop solution for HTTP(S) testing." __all__ = ["__version__", "__description__"] diff --git a/httprunner/api.py b/httprunner/api.py index af44847a..6f5cd568 100644 --- a/httprunner/api.py +++ b/httprunner/api.py @@ -1,3 +1,4 @@ +import os import unittest from httprunner import (__version__, exceptions, loader, logger, parser, @@ -27,6 +28,7 @@ class HttpRunner(object): self.test_loader = unittest.TestLoader() self.save_tests = save_tests self._summary = None + self.project_working_directory = None def _add_tests(self, testcases): """ initialize testcase with Runner() and add to test suite. @@ -166,6 +168,8 @@ class HttpRunner(object): """ run testcase/testsuite data """ project_mapping = tests_mapping.get("project_mapping", {}) + self.project_working_directory = project_mapping.get("PWD", os.getcwd()) + if self.save_tests: utils.dump_logs(tests_mapping, project_mapping, "loaded") diff --git a/httprunner/cli.py b/httprunner/cli.py index 4ee419e4..080a125f 100644 --- a/httprunner/cli.py +++ b/httprunner/cli.py @@ -1,4 +1,5 @@ import argparse +import os import sys from httprunner import __description__, __version__ @@ -41,7 +42,7 @@ def main(): help="specify report save directory.") parser.add_argument( '--report-file', - help="specify report file name.") + help="specify report file path, this has higher priority than specifying report dir.") parser.add_argument( '--failfast', action='store_true', default=False, help="Stop the test run on the first error or failure.") @@ -91,10 +92,11 @@ def main(): try: for path in args.testcase_paths: summary = runner.run(path, dot_env_path=args.dot_env_path) + report_dir = args.report_dir or os.path.join(runner.project_working_directory, "reports") render_html_report( summary, args.report_template, - args.report_dir, + report_dir, args.report_file ) except Exception: diff --git a/httprunner/report.py b/httprunner/report.py index 72a6f9c7..e957533c 100644 --- a/httprunner/report.py +++ b/httprunner/report.py @@ -282,6 +282,7 @@ def render_html_report(summary, report_template=None, report_dir=None, report_fi Args: report_template (str): specify html report template path, template should be in Jinja2 format. report_dir (str): specify html report save directory + report_file (str): specify html report file path, this has higher priority than specifying report dir. """ if not report_template: @@ -296,18 +297,20 @@ def render_html_report(summary, report_template=None, report_dir=None, report_fi logger.log_info("Start to render Html report ...") - report_dir = report_dir or os.path.join(os.getcwd(), "reports") - if not os.path.isdir(report_dir): - os.makedirs(report_dir) - start_at_timestamp = int(summary["time"]["start_at"]) summary["time"]["start_datetime"] = datetime.fromtimestamp(start_at_timestamp).strftime('%Y-%m-%d %H:%M:%S') if report_file: - report_path = os.path.join(report_dir, report_file) + report_dir = os.path.dirname(report_file) + report_file_name = os.path.basename(report_file) else: - report_path = os.path.join(report_dir, "{}.html".format(start_at_timestamp)) + report_dir = report_dir or os.path.join(os.getcwd(), "reports") + report_file_name = "{}.html".format(start_at_timestamp) + if not os.path.isdir(report_dir): + os.makedirs(report_dir) + + report_path = os.path.join(report_dir, report_file_name) with io.open(report_template, "r", encoding='utf-8') as fp_r: template_content = fp_r.read() with io.open(report_path, 'w', encoding='utf-8') as fp_w: