Skip to content

Commit d503176

Browse files
Joep Schuurkesdavehunt
authored andcommitted
Add hook for modifying summary section (#152)
Closes #109
1 parent abf8f15 commit d503176

4 files changed

Lines changed: 47 additions & 2 deletions

File tree

README.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ via the :code:`pytest_configure` hook:
8585
def pytest_configure(config):
8686
config._metadata['foo'] = 'bar'
8787
88+
Additional summary information
89+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90+
91+
You can edit the *Summary* section by using the :code:`pytest_html_results_summary` hook:
92+
93+
.. code-block:: python
94+
95+
import pytest
96+
from py.xml import html
97+
98+
@pytest.mark.optionalhook
99+
def pytest_html_results_summary(prefix, summary, postfix):
100+
prefix.extend([html.p("foo: bar")])
101+
88102
Extra content
89103
~~~~~~~~~~~~~
90104

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_results_summary(prefix, summary, postfix):
7+
""" Called before adding the summary section to the report """
8+
9+
610
def pytest_html_results_table_header(cells):
711
""" Called after building results table header. """
812

pytest_html/plugin.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def generate_summary_item(self):
376376
if self.rerun is not None:
377377
outcomes.append(Outcome('rerun', self.rerun))
378378

379-
summary = [html.h2('Summary'), html.p(
379+
summary = [html.p(
380380
'{0} tests ran in {1:.2f} seconds. '.format(
381381
numtests, suite_time_delta)),
382382
html.p('(Un)check the boxes to filter the results.',
@@ -423,7 +423,13 @@ def generate_summary_item(self):
423423
onLoad='init()')
424424

425425
body.extend(self._generate_environment(session.config))
426-
body.extend(summary)
426+
427+
summary_prefix, summary_postfix = [], []
428+
session.config.hook.pytest_html_results_summary(
429+
prefix=summary_prefix, summary=summary, postfix=summary_postfix)
430+
body.extend([html.h2('Summary')] + summary_prefix
431+
+ summary + summary_postfix)
432+
427433
body.extend(results)
428434

429435
doc = html.html(head, body)

testing/test_pytest_html.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,27 @@ def test_stdout():
229229
assert content not in html
230230
assert escaped in html
231231

232+
def test_custom_content_in_summary(self, testdir):
233+
content_prefix = str(random.random())
234+
content_summary = str(random.random())
235+
content_suffix = str(random.random())
236+
testdir.makeconftest("""
237+
import pytest
238+
from py.xml import html
239+
240+
@pytest.mark.optionalhook
241+
def pytest_html_results_summary(prefix, summary, postfix):
242+
prefix.append(html.p("prefix is {0}"))
243+
summary.extend([html.p("extra summary is {1}")])
244+
postfix.extend([html.p("postfix is {2}")])
245+
""".format(content_prefix, content_summary, content_suffix))
246+
testdir.makepyfile('def test_pass(): pass')
247+
result, html = run(testdir)
248+
assert result.ret == 0
249+
assert len(re.findall(content_prefix, html)) == 1
250+
assert len(re.findall(content_summary, html)) == 1
251+
assert len(re.findall(content_suffix, html)) == 1
252+
232253
def test_extra_html(self, testdir):
233254
content = str(random.random())
234255
testdir.makeconftest("""

0 commit comments

Comments
 (0)