Add hook for modifying summary section (#152)

Closes #109
This commit is contained in:
Joep Schuurkes 2018-04-05 12:31:20 +02:00 committed by Dave Hunt
parent abf8f15e4e
commit d5031769b6
4 changed files with 47 additions and 2 deletions

View File

@ -85,6 +85,20 @@ via the :code:`pytest_configure` hook:
def pytest_configure(config):
config._metadata['foo'] = 'bar'
Additional summary information
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can edit the *Summary* section by using the :code:`pytest_html_results_summary` hook:
.. code-block:: python
import pytest
from py.xml import html
@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
prefix.extend([html.p("foo: bar")])
Extra content
~~~~~~~~~~~~~

View File

@ -3,6 +3,10 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
def pytest_html_results_summary(prefix, summary, postfix):
""" Called before adding the summary section to the report """
def pytest_html_results_table_header(cells):
""" Called after building results table header. """

View File

@ -376,7 +376,7 @@ class HTMLReport(object):
if self.rerun is not None:
outcomes.append(Outcome('rerun', self.rerun))
summary = [html.h2('Summary'), html.p(
summary = [html.p(
'{0} tests ran in {1:.2f} seconds. '.format(
numtests, suite_time_delta)),
html.p('(Un)check the boxes to filter the results.',
@ -423,7 +423,13 @@ class HTMLReport(object):
onLoad='init()')
body.extend(self._generate_environment(session.config))
body.extend(summary)
summary_prefix, summary_postfix = [], []
session.config.hook.pytest_html_results_summary(
prefix=summary_prefix, summary=summary, postfix=summary_postfix)
body.extend([html.h2('Summary')] + summary_prefix
+ summary + summary_postfix)
body.extend(results)
doc = html.html(head, body)

View File

@ -229,6 +229,27 @@ class TestHTML:
assert content not in html
assert escaped in html
def test_custom_content_in_summary(self, testdir):
content_prefix = str(random.random())
content_summary = str(random.random())
content_suffix = str(random.random())
testdir.makeconftest("""
import pytest
from py.xml import html
@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
prefix.append(html.p("prefix is {0}"))
summary.extend([html.p("extra summary is {1}")])
postfix.extend([html.p("postfix is {2}")])
""".format(content_prefix, content_summary, content_suffix))
testdir.makepyfile('def test_pass(): pass')
result, html = run(testdir)
assert result.ret == 0
assert len(re.findall(content_prefix, html)) == 1
assert len(re.findall(content_summary, html)) == 1
assert len(re.findall(content_suffix, html)) == 1
def test_extra_html(self, testdir):
content = str(random.random())
testdir.makeconftest("""