Skip to content

Add hook to change report main title #270

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

Merged
merged 1 commit into from
Feb 28, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ be used to change the appearance of the report.

$ pytest --html=report.html --css=highcontrast.css --css=accessible.css

Report Title
~~~~~~~~~~~~

By default report title will be the filename of the report, you can edit it by using the :code: `pytest_html_report_title` hook:

.. code-block:: python

import pytest
from py.xml import html

def pytest_html_report_title(report)
report.title = "My very own title!"

Environment
~~~~~~~~~~~

Expand Down
4 changes: 4 additions & 0 deletions pytest_html/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


def pytest_html_report_title(report):
""" Called before adding the title to the report """


def pytest_html_results_summary(prefix, summary, postfix):
""" Called before adding the summary section to the report """

Expand Down
5 changes: 4 additions & 1 deletion pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def __init__(self, logfile, config):
logfile = os.path.expanduser(os.path.expandvars(logfile))
self.logfile = os.path.abspath(logfile)
self.test_logs = []
self.title = os.path.basename(self.logfile)
self.results = []
self.errors = self.failed = 0
self.passed = self.skipped = 0
Expand Down Expand Up @@ -490,9 +491,11 @@ def generate_summary_item(self):
__name__, os.path.join("resources", "main.js")
).decode("utf-8")

session.config.hook.pytest_html_report_title(report=self)

body = html.body(
html.script(raw(main_js)),
html.h1(os.path.basename(self.logfile)),
html.h1(self.title),
html.p(
"Report generated on {} at {} by ".format(
generated.strftime("%d-%b-%Y"), generated.strftime("%H:%M:%S")
Expand Down
16 changes: 16 additions & 0 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,3 +904,19 @@ def test_pass():
assert result.ret == 1
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