Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the report tab title reflect the report name #420

Merged
merged 2 commits into from Dec 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.rst
@@ -1,6 +1,12 @@
Release Notes
-------------

**3.2.0 (unreleased)**

* Make the report tab title reflect the report name. (`#412 <https://github.com/pytest-dev/pytest-html/issues/412>`_)

* Thanks to `@gnikonorov <https://github.com/gnikonorov>`_ for the PR

**3.1.1 (2020-12-13)**

* Fix issue with reporting of missing CSS files. (`#388 <https://github.com/pytest-dev/pytest-html/issues/388>`_)
Expand Down
8 changes: 3 additions & 5 deletions src/pytest_html/plugin.py
Expand Up @@ -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__(
Expand Down Expand Up @@ -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),
Expand Down
43 changes: 23 additions & 20 deletions testing/test_pytest_html.py
Expand Up @@ -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"<h1>{report_name}</h1>"
assert report_title in html

report_head_title_string = f"<title>{report_title}</title>"
assert len(re.findall(report_head_title_string, html)) == 1, html

report_body_title_string = f"<h1>{report_title}</h1>"
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"
Expand Down Expand Up @@ -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(
"""
Expand Down