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..73df0438 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -304,15 +304,34 @@ 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") + report_name = "report.html" + report_title = "My Custom Report" if is_custom else report_name + if is_custom: + testdir.makeconftest( + f""" + import pytest + from py.xml import html + + def pytest_html_report_title(report): + report.title = "{report_title}" + """ + ) + 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"{report_title}" + assert len(re.findall(report_head_title_string, html)) == 1, html + + 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): report_location = "REPORT_LOCATION" @@ -1073,22 +1092,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( """