Skip to content

Commit

Permalink
Merge pull request pytest-dev#270 from werdeil/master
Browse files Browse the repository at this point in the history
Add hook to change report main title
  • Loading branch information
BeyondEvil authored Feb 28, 2020
2 parents 593bbda + 966228c commit ae0ca62
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
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

0 comments on commit ae0ca62

Please sign in to comment.