From ac5424cfde0769e74aff16970c7a7da3fcd1c9b6 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sun, 13 Dec 2020 11:58:13 -0500 Subject: [PATCH 1/2] Make the report tab title reflect the report name --- CHANGES.rst | 6 +++++ src/pytest_html/plugin.py | 8 +++--- testing/test_pytest_html.py | 51 ++++++++++++++++++++++--------------- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 956c4ac2..58ef255a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,12 @@ Release Notes ------------- +**3.2.0 (unreleased)** + +* Make the report tab title reflect the report name. (`#412 `_) + + * Thanks to `@gnikonorov `_ for the PR + **3.1.1 (2020-12-13)** * Fix issue with reporting of missing CSS files. (`#388 `_) diff --git a/src/pytest_html/plugin.py b/src/pytest_html/plugin.py index 7bbee732..b89d2b56 100644 --- a/src/pytest_html/plugin.py +++ b/src/pytest_html/plugin.py @@ -510,9 +510,9 @@ def _generate_report(self, session): if self.self_contained: html_css = html.style(raw(self.style_css)) - head = html.head( - html.meta(charset="utf-8"), html.title("Test Report"), html_css - ) + session.config.hook.pytest_html_report_title(report=self) + + head = html.head(html.meta(charset="utf-8"), html.title(self.title), html_css) class Outcome: def __init__( @@ -611,8 +611,6 @@ def generate_summary_item(self): ) as main_js_fp: main_js = main_js_fp.read() - session.config.hook.pytest_html_report_title(report=self) - body = html.body( html.script(raw(main_js)), html.h1(self.title), diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index 2bab9a19..e393f25a 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -304,15 +304,42 @@ def test_create_report_path(self, testdir): assert result.ret == 0 assert_results(html) - @pytest.mark.parametrize("path", ["", "directory"]) - def test_report_title(self, testdir, path): + @pytest.mark.parametrize( + "path, is_custom", [("", False), ("", True), ("directory", False)] + ) + def test_report_title(self, testdir, path, is_custom): testdir.makepyfile("def test_pass(): pass") + + custom_report_title = "My Custom Report" + if is_custom: + testdir.makeconftest( + f""" + import pytest + from py.xml import html + + def pytest_html_report_title(report): + report.title = "{custom_report_title}" + """ + ) + report_name = "report.html" path = os.path.join(path, report_name) result, html = run(testdir, path) assert result.ret == 0 - report_title = f"

{report_name}

" - assert report_title in html + + report_head_title_string = ( + f"{custom_report_title}" + if is_custom + else f"{report_name}" + ) + assert len(re.findall(report_head_title_string, html)) == 1, html + + report_body_title_string = ( + f"

{custom_report_title}

" + if is_custom + else f"

{report_name}

" + ) + assert len(re.findall(report_body_title_string, html)) == 1, html def test_report_title_addopts_env_var(self, testdir, monkeypatch): report_location = "REPORT_LOCATION" @@ -1073,22 +1100,6 @@ def test_pass(): assert len(re.findall(collapsed_html, html)) == expected_count assert_results(html, tests=2, passed=1, failed=1) - def test_custom_content_report_title(self, testdir): - content_report_title = str(random.random()) - testdir.makeconftest( - f""" - import pytest - from py.xml import html - - def pytest_html_report_title(report): - report.title = "title is {content_report_title}" - """ - ) - testdir.makepyfile("def test_pass(): pass") - result, html = run(testdir) - assert result.ret == 0 - assert len(re.findall(content_report_title, html)) == 1 - def test_setup_and_teardown_in_html(self, testdir): testdir.makepyfile( """ From a1da94929fc00b6c1a0c0081d14524877a47eeeb Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sun, 13 Dec 2020 17:03:42 -0500 Subject: [PATCH 2/2] refactor test_report_title test --- testing/test_pytest_html.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index e393f25a..73df0438 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -310,7 +310,8 @@ def test_create_report_path(self, testdir): def test_report_title(self, testdir, path, is_custom): testdir.makepyfile("def test_pass(): pass") - custom_report_title = "My Custom Report" + report_name = "report.html" + report_title = "My Custom Report" if is_custom else report_name if is_custom: testdir.makeconftest( f""" @@ -318,27 +319,18 @@ def test_report_title(self, testdir, path, is_custom): from py.xml import html def pytest_html_report_title(report): - report.title = "{custom_report_title}" + report.title = "{report_title}" """ ) - report_name = "report.html" path = os.path.join(path, report_name) result, html = run(testdir, path) assert result.ret == 0 - report_head_title_string = ( - f"{custom_report_title}" - if is_custom - else f"{report_name}" - ) + report_head_title_string = f"{report_title}" assert len(re.findall(report_head_title_string, html)) == 1, html - report_body_title_string = ( - f"

{custom_report_title}

" - if is_custom - else f"

{report_name}

" - ) + report_body_title_string = f"

{report_title}

" assert len(re.findall(report_body_title_string, html)) == 1, html def test_report_title_addopts_env_var(self, testdir, monkeypatch):