Introduce new pytest_report_collectionfinish hook

Fix #2622
This commit is contained in:
Bruno Oliveira 2017-07-27 10:34:49 -03:00
parent ddf1751e6d
commit 17c544e793
5 changed files with 41 additions and 5 deletions

View File

@ -337,7 +337,10 @@ def pytest_assertrepr_compare(config, op, left, right):
def pytest_report_header(config, startdir): def pytest_report_header(config, startdir):
""" return a string to be displayed as header info for terminal reporting. """ return a string or list of strings to be displayed as header info for terminal reporting.
:param config: the pytest config object.
:param startdir: py.path object with the starting dir
.. note:: .. note::
@ -347,6 +350,17 @@ def pytest_report_header(config, startdir):
""" """
def pytest_report_collectionfinish(config, startdir, items):
""" return a string or list of strings to be displayed after collection has finished successfully.
This strings will be displayed after the standard "collected X items" message.
:param config: the pytest config object.
:param startdir: py.path object with the starting dir
:param items: list of pytest items that are going to be executed; this list should not be modified.
"""
@hookspec(firstresult=True) @hookspec(firstresult=True)
def pytest_report_teststatus(report): def pytest_report_teststatus(report):
""" return result-category, shortletter and verbose word for reporting. """ return result-category, shortletter and verbose word for reporting.

View File

@ -323,6 +323,9 @@ class TerminalReporter:
self.write_line(msg) self.write_line(msg)
lines = self.config.hook.pytest_report_header( lines = self.config.hook.pytest_report_header(
config=self.config, startdir=self.startdir) config=self.config, startdir=self.startdir)
self._write_report_lines_from_hooks(lines)
def _write_report_lines_from_hooks(self, lines):
lines.reverse() lines.reverse()
for line in flatten(lines): for line in flatten(lines):
self.write_line(line) self.write_line(line)
@ -349,10 +352,9 @@ class TerminalReporter:
rep.toterminal(self._tw) rep.toterminal(self._tw)
return 1 return 1
return 0 return 0
if not self.showheader: lines = self.config.hook.pytest_report_collectionfinish(
return config=self.config, startdir=self.startdir, items=session.items)
# for i, testarg in enumerate(self.config.args): self._write_report_lines_from_hooks(lines)
# self.write_line("test path %d: %s" %(i+1, testarg))
def _printcollecteditems(self, items): def _printcollecteditems(self, items):
# to print out items and their parent collectors # to print out items and their parent collectors

2
changelog/2622.feature Normal file
View File

@ -0,0 +1,2 @@
New ``pytest_report_collectionfinish`` hook which allows plugins to add messages to the terminal reporting after
collection has been finished successfully.

View File

@ -644,6 +644,7 @@ Session related reporting hooks:
.. autofunction:: pytest_collectreport .. autofunction:: pytest_collectreport
.. autofunction:: pytest_deselected .. autofunction:: pytest_deselected
.. autofunction:: pytest_report_header .. autofunction:: pytest_report_header
.. autofunction:: pytest_report_collectionfinish
.. autofunction:: pytest_report_teststatus .. autofunction:: pytest_report_teststatus
.. autofunction:: pytest_terminal_summary .. autofunction:: pytest_terminal_summary
.. autofunction:: pytest_fixture_setup .. autofunction:: pytest_fixture_setup

View File

@ -544,6 +544,23 @@ class TestTerminalFunctional(object):
assert "===" not in s assert "===" not in s
assert "passed" not in s assert "passed" not in s
def test_report_collectionfinish_hook(self, testdir):
testdir.makeconftest("""
def pytest_report_collectionfinish(config, startdir, items):
return ['hello from hook: {0} items'.format(len(items))]
""")
testdir.makepyfile("""
import pytest
@pytest.mark.parametrize('i', range(3))
def test(i):
pass
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines([
"collected 3 items",
"hello from hook: 3 items",
])
def test_fail_extra_reporting(testdir): def test_fail_extra_reporting(testdir):
testdir.makepyfile("def test_this(): assert 0") testdir.makepyfile("def test_this(): assert 0")