Skip to content

Commit

Permalink
Merge pull request #2623 from nicoddemus/post-collection-report-hook
Browse files Browse the repository at this point in the history
Introduce new pytest_report_collectionfinish hook
  • Loading branch information
RonnyPfannschmidt committed Jul 27, 2017
2 parents ddf1751 + 17c544e commit e97fd5e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
16 changes: 15 additions & 1 deletion _pytest/hookspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@ def pytest_assertrepr_compare(config, op, left, right):


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::
Expand All @@ -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)
def pytest_report_teststatus(report):
""" return result-category, shortletter and verbose word for reporting.
Expand Down
10 changes: 6 additions & 4 deletions _pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ def pytest_sessionstart(self, session):
self.write_line(msg)
lines = self.config.hook.pytest_report_header(
config=self.config, startdir=self.startdir)
self._write_report_lines_from_hooks(lines)

def _write_report_lines_from_hooks(self, lines):
lines.reverse()
for line in flatten(lines):
self.write_line(line)
Expand All @@ -349,10 +352,9 @@ def pytest_collection_finish(self, session):
rep.toterminal(self._tw)
return 1
return 0
if not self.showheader:
return
# for i, testarg in enumerate(self.config.args):
# self.write_line("test path %d: %s" %(i+1, testarg))
lines = self.config.hook.pytest_report_collectionfinish(
config=self.config, startdir=self.startdir, items=session.items)
self._write_report_lines_from_hooks(lines)

def _printcollecteditems(self, items):
# to print out items and their parent collectors
Expand Down
2 changes: 2 additions & 0 deletions changelog/2622.feature
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions doc/en/writing_plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ Session related reporting hooks:
.. autofunction:: pytest_collectreport
.. autofunction:: pytest_deselected
.. autofunction:: pytest_report_header
.. autofunction:: pytest_report_collectionfinish
.. autofunction:: pytest_report_teststatus
.. autofunction:: pytest_terminal_summary
.. autofunction:: pytest_fixture_setup
Expand Down
17 changes: 17 additions & 0 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,23 @@ def test_more_quiet_reporting(self, testdir):
assert "===" 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):
testdir.makepyfile("def test_this(): assert 0")
Expand Down

0 comments on commit e97fd5e

Please sign in to comment.