Skip to content

Commit e260972

Browse files
authored
Make the report tab title reflect the report name (#420)
* Make the report tab title reflect the report name * refactor test_report_title test
1 parent a1639ef commit e260972

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Release Notes
22
-------------
33

4+
**3.2.0 (unreleased)**
5+
6+
* Make the report tab title reflect the report name. (`#412 <https://github.com/pytest-dev/pytest-html/issues/412>`_)
7+
8+
* Thanks to `@gnikonorov <https://github.com/gnikonorov>`_ for the PR
9+
410
**3.1.1 (2020-12-13)**
511

612
* Fix issue with reporting of missing CSS files. (`#388 <https://github.com/pytest-dev/pytest-html/issues/388>`_)

src/pytest_html/plugin.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,9 @@ def _generate_report(self, session):
510510
if self.self_contained:
511511
html_css = html.style(raw(self.style_css))
512512

513-
head = html.head(
514-
html.meta(charset="utf-8"), html.title("Test Report"), html_css
515-
)
513+
session.config.hook.pytest_html_report_title(report=self)
514+
515+
head = html.head(html.meta(charset="utf-8"), html.title(self.title), html_css)
516516

517517
class Outcome:
518518
def __init__(
@@ -611,8 +611,6 @@ def generate_summary_item(self):
611611
) as main_js_fp:
612612
main_js = main_js_fp.read()
613613

614-
session.config.hook.pytest_html_report_title(report=self)
615-
616614
body = html.body(
617615
html.script(raw(main_js)),
618616
html.h1(self.title),

testing/test_pytest_html.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,34 @@ def test_create_report_path(self, testdir):
304304
assert result.ret == 0
305305
assert_results(html)
306306

307-
@pytest.mark.parametrize("path", ["", "directory"])
308-
def test_report_title(self, testdir, path):
307+
@pytest.mark.parametrize(
308+
"path, is_custom", [("", False), ("", True), ("directory", False)]
309+
)
310+
def test_report_title(self, testdir, path, is_custom):
309311
testdir.makepyfile("def test_pass(): pass")
312+
310313
report_name = "report.html"
314+
report_title = "My Custom Report" if is_custom else report_name
315+
if is_custom:
316+
testdir.makeconftest(
317+
f"""
318+
import pytest
319+
from py.xml import html
320+
321+
def pytest_html_report_title(report):
322+
report.title = "{report_title}"
323+
"""
324+
)
325+
311326
path = os.path.join(path, report_name)
312327
result, html = run(testdir, path)
313328
assert result.ret == 0
314-
report_title = f"<h1>{report_name}</h1>"
315-
assert report_title in html
329+
330+
report_head_title_string = f"<title>{report_title}</title>"
331+
assert len(re.findall(report_head_title_string, html)) == 1, html
332+
333+
report_body_title_string = f"<h1>{report_title}</h1>"
334+
assert len(re.findall(report_body_title_string, html)) == 1, html
316335

317336
def test_report_title_addopts_env_var(self, testdir, monkeypatch):
318337
report_location = "REPORT_LOCATION"
@@ -1073,22 +1092,6 @@ def test_pass():
10731092
assert len(re.findall(collapsed_html, html)) == expected_count
10741093
assert_results(html, tests=2, passed=1, failed=1)
10751094

1076-
def test_custom_content_report_title(self, testdir):
1077-
content_report_title = str(random.random())
1078-
testdir.makeconftest(
1079-
f"""
1080-
import pytest
1081-
from py.xml import html
1082-
1083-
def pytest_html_report_title(report):
1084-
report.title = "title is {content_report_title}"
1085-
"""
1086-
)
1087-
testdir.makepyfile("def test_pass(): pass")
1088-
result, html = run(testdir)
1089-
assert result.ret == 0
1090-
assert len(re.findall(content_report_title, html)) == 1
1091-
10921095
def test_setup_and_teardown_in_html(self, testdir):
10931096
testdir.makepyfile(
10941097
"""

0 commit comments

Comments
 (0)