Skip to content

Commit e97fd5e

Browse files
Merge pull request #2623 from nicoddemus/post-collection-report-hook
Introduce new pytest_report_collectionfinish hook
2 parents ddf1751 + 17c544e commit e97fd5e

5 files changed

Lines changed: 41 additions & 5 deletions

File tree

_pytest/hookspec.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,10 @@ def pytest_assertrepr_compare(config, op, left, right):
337337

338338

339339
def pytest_report_header(config, startdir):
340-
""" return a string to be displayed as header info for terminal reporting.
340+
""" return a string or list of strings to be displayed as header info for terminal reporting.
341+
342+
:param config: the pytest config object.
343+
:param startdir: py.path object with the starting dir
341344
342345
.. note::
343346
@@ -347,6 +350,17 @@ def pytest_report_header(config, startdir):
347350
"""
348351

349352

353+
def pytest_report_collectionfinish(config, startdir, items):
354+
""" return a string or list of strings to be displayed after collection has finished successfully.
355+
356+
This strings will be displayed after the standard "collected X items" message.
357+
358+
:param config: the pytest config object.
359+
:param startdir: py.path object with the starting dir
360+
:param items: list of pytest items that are going to be executed; this list should not be modified.
361+
"""
362+
363+
350364
@hookspec(firstresult=True)
351365
def pytest_report_teststatus(report):
352366
""" return result-category, shortletter and verbose word for reporting.

_pytest/terminal.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ def pytest_sessionstart(self, session):
323323
self.write_line(msg)
324324
lines = self.config.hook.pytest_report_header(
325325
config=self.config, startdir=self.startdir)
326+
self._write_report_lines_from_hooks(lines)
327+
328+
def _write_report_lines_from_hooks(self, lines):
326329
lines.reverse()
327330
for line in flatten(lines):
328331
self.write_line(line)
@@ -349,10 +352,9 @@ def pytest_collection_finish(self, session):
349352
rep.toterminal(self._tw)
350353
return 1
351354
return 0
352-
if not self.showheader:
353-
return
354-
# for i, testarg in enumerate(self.config.args):
355-
# self.write_line("test path %d: %s" %(i+1, testarg))
355+
lines = self.config.hook.pytest_report_collectionfinish(
356+
config=self.config, startdir=self.startdir, items=session.items)
357+
self._write_report_lines_from_hooks(lines)
356358

357359
def _printcollecteditems(self, items):
358360
# to print out items and their parent collectors

changelog/2622.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
New ``pytest_report_collectionfinish`` hook which allows plugins to add messages to the terminal reporting after
2+
collection has been finished successfully.

doc/en/writing_plugins.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ Session related reporting hooks:
644644
.. autofunction:: pytest_collectreport
645645
.. autofunction:: pytest_deselected
646646
.. autofunction:: pytest_report_header
647+
.. autofunction:: pytest_report_collectionfinish
647648
.. autofunction:: pytest_report_teststatus
648649
.. autofunction:: pytest_terminal_summary
649650
.. autofunction:: pytest_fixture_setup

testing/test_terminal.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,23 @@ def test_more_quiet_reporting(self, testdir):
544544
assert "===" not in s
545545
assert "passed" not in s
546546

547+
def test_report_collectionfinish_hook(self, testdir):
548+
testdir.makeconftest("""
549+
def pytest_report_collectionfinish(config, startdir, items):
550+
return ['hello from hook: {0} items'.format(len(items))]
551+
""")
552+
testdir.makepyfile("""
553+
import pytest
554+
@pytest.mark.parametrize('i', range(3))
555+
def test(i):
556+
pass
557+
""")
558+
result = testdir.runpytest()
559+
result.stdout.fnmatch_lines([
560+
"collected 3 items",
561+
"hello from hook: 3 items",
562+
])
563+
547564

548565
def test_fail_extra_reporting(testdir):
549566
testdir.makepyfile("def test_this(): assert 0")

0 commit comments

Comments
 (0)