Skip to content

Commit 966228c

Browse files
committed
Add hook to change report main title
1 parent 593bbda commit 966228c

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

README.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ be used to change the appearance of the report.
8787
8888
$ pytest --html=report.html --css=highcontrast.css --css=accessible.css
8989
90+
Report Title
91+
~~~~~~~~~~~~
92+
93+
By default report title will be the filename of the report, you can edit it by using the :code: `pytest_html_report_title` hook:
94+
95+
.. code-block:: python
96+
97+
import pytest
98+
from py.xml import html
99+
100+
def pytest_html_report_title(report)
101+
report.title = "My very own title!"
102+
90103
Environment
91104
~~~~~~~~~~~
92105

pytest_html/hooks.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55

6+
def pytest_html_report_title(report):
7+
""" Called before adding the title to the report """
8+
9+
610
def pytest_html_results_summary(prefix, summary, postfix):
711
""" Called before adding the summary section to the report """
812

pytest_html/plugin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def __init__(self, logfile, config):
9999
logfile = os.path.expanduser(os.path.expandvars(logfile))
100100
self.logfile = os.path.abspath(logfile)
101101
self.test_logs = []
102+
self.title = os.path.basename(self.logfile)
102103
self.results = []
103104
self.errors = self.failed = 0
104105
self.passed = self.skipped = 0
@@ -490,9 +491,11 @@ def generate_summary_item(self):
490491
__name__, os.path.join("resources", "main.js")
491492
).decode("utf-8")
492493

494+
session.config.hook.pytest_html_report_title(report=self)
495+
493496
body = html.body(
494497
html.script(raw(main_js)),
495-
html.h1(os.path.basename(self.logfile)),
498+
html.h1(self.title),
496499
html.p(
497500
"Report generated on {} at {} by ".format(
498501
generated.strftime("%d-%b-%Y"), generated.strftime("%H:%M:%S")

testing/test_pytest_html.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,19 @@ def test_pass():
904904
assert result.ret == 1
905905
assert len(re.findall(collapsed_html, html)) == expected_count
906906
assert_results(html, tests=2, passed=1, failed=1)
907+
908+
def test_custom_content_report_title(self, testdir):
909+
content_report_title = str(random.random())
910+
testdir.makeconftest(
911+
f"""
912+
import pytest
913+
from py.xml import html
914+
915+
def pytest_html_report_title(report):
916+
report.title = "title is {content_report_title}"
917+
"""
918+
)
919+
testdir.makepyfile("def test_pass(): pass")
920+
result, html = run(testdir)
921+
assert result.ret == 0
922+
assert len(re.findall(content_report_title, html)) == 1

0 commit comments

Comments
 (0)