Skip to content

Commit 54af0f4

Browse files
committed
Call pytest_report_collectionfinish hook when --collect-only is passed
Fix #2895
1 parent a945734 commit 54af0f4

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

changelog/2895.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The ``pytest_report_collectionfinish`` hook now is also called with ``--collect-only``.

src/_pytest/terminal.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,19 +574,20 @@ def pytest_report_header(self, config):
574574
return lines
575575

576576
def pytest_collection_finish(self, session):
577-
if self.config.option.collectonly:
577+
if self.config.getoption("collectonly"):
578578
self._printcollecteditems(session.items)
579-
if self.stats.get("failed"):
580-
self._tw.sep("!", "collection failures")
581-
for rep in self.stats.get("failed"):
582-
rep.toterminal(self._tw)
583-
return 1
584-
return 0
579+
585580
lines = self.config.hook.pytest_report_collectionfinish(
586581
config=self.config, startdir=self.startdir, items=session.items
587582
)
588583
self._write_report_lines_from_hooks(lines)
589584

585+
if self.config.getoption("collectonly"):
586+
if self.stats.get("failed"):
587+
self._tw.sep("!", "collection failures")
588+
for rep in self.stats.get("failed"):
589+
rep.toterminal(self._tw)
590+
590591
def _printcollecteditems(self, items):
591592
# to print out items and their parent collectors
592593
# we take care to leave out Instances aka ()

testing/test_terminal.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,10 @@ def test_more_quiet_reporting(self, testdir):
649649
assert "===" not in s
650650
assert "passed" not in s
651651

652-
def test_report_collectionfinish_hook(self, testdir):
652+
@pytest.mark.parametrize(
653+
"params", [(), ("--collect-only",)], ids=["no-params", "collect-only"]
654+
)
655+
def test_report_collectionfinish_hook(self, testdir, params):
653656
testdir.makeconftest(
654657
"""
655658
def pytest_report_collectionfinish(config, startdir, items):
@@ -664,7 +667,7 @@ def test(i):
664667
pass
665668
"""
666669
)
667-
result = testdir.runpytest()
670+
result = testdir.runpytest(*params)
668671
result.stdout.fnmatch_lines(["collected 3 items", "hello from hook: 3 items"])
669672

670673

0 commit comments

Comments
 (0)