From 582c29fb5a5c00fa7a7a7b64dd869e65823302ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Fri, 26 Apr 2019 11:08:20 +0200 Subject: [PATCH] Handle when report title is stored as an environment variable When the --html flag is used in pytest.ini with "addopts" and the report title is stored in an environment variable, the name of the environment variable was used as the report title. In other words, the environment variable was never expanded. Fixes: #201 --- .gitignore | 2 ++ pytest_html/plugin.py | 2 +- testing/test_pytest_html.py | 26 ++++++++++++++++++++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index cb6ef941..b89f6565 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,5 @@ local.properties ##Tests JS node_modules/ + +Pipfile diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index fa177de1..e66840de 100644 --- a/pytest_html/plugin.py +++ b/pytest_html/plugin.py @@ -432,7 +432,7 @@ def generate_summary_item(self): body = html.body( html.script(raw(main_js)), - html.h1(os.path.basename(session.config.option.htmlpath)), + html.h1(os.path.basename(self.logfile)), html.p('Report generated on {0} at {1} by '.format( generated.strftime('%d-%b-%Y'), generated.strftime('%H:%M:%S')), diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index c8165198..eedd44d8 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -21,9 +21,12 @@ def run(testdir, path='report.html', *args): path = testdir.tmpdir.join(path) result = testdir.runpytest('--html', path, *args) + return result, read_html(path) + + +def read_html(path): with open(str(path)) as f: - html = f.read() - return result, html + return f.read() def assert_results_by_outcome(html, test_outcome, test_outcome_number, @@ -193,6 +196,25 @@ def test_report_title(self, testdir, path): report_title = "

{0}

".format(report_name) assert report_title in html + def test_report_title_addopts_env_var(self, testdir, monkeypatch): + report_location = "REPORT_LOCATION" + report_name = "MuhReport" + monkeypatch.setenv(report_location, report_name) + testdir.makefile( + ".ini", + pytest=""" + [pytest] + addopts = --html ${0} + """.format( + report_location + ), + ) + testdir.makepyfile('def test_pass(): pass') + result = testdir.runpytest() + assert result.ret == 0 + report_title = "

{0}

".format(report_name) + assert report_title in read_html(report_name) + def test_resources_inline_css(self, testdir): testdir.makepyfile('def test_pass(): pass') result, html = run(testdir, 'report.html', '--self-contained-html')