Merge pull request #4725 from nicoddemus/collection-finish

Call pytest_report_collectionfinish hook when --collect-only is passed
This commit is contained in:
Zac Hatfield-Dodds 2019-02-06 12:38:12 -10:00 committed by GitHub
commit 3384ffc6eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 9 deletions

View File

@ -0,0 +1 @@
The ``pytest_report_collectionfinish`` hook now is also called with ``--collect-only``.

View File

@ -574,19 +574,20 @@ class TerminalReporter(object):
return lines
def pytest_collection_finish(self, session):
if self.config.option.collectonly:
if self.config.getoption("collectonly"):
self._printcollecteditems(session.items)
if self.stats.get("failed"):
self._tw.sep("!", "collection failures")
for rep in self.stats.get("failed"):
rep.toterminal(self._tw)
return 1
return 0
lines = self.config.hook.pytest_report_collectionfinish(
config=self.config, startdir=self.startdir, items=session.items
)
self._write_report_lines_from_hooks(lines)
if self.config.getoption("collectonly"):
if self.stats.get("failed"):
self._tw.sep("!", "collection failures")
for rep in self.stats.get("failed"):
rep.toterminal(self._tw)
def _printcollecteditems(self, items):
# to print out items and their parent collectors
# we take care to leave out Instances aka ()

View File

@ -649,7 +649,10 @@ class TestTerminalFunctional(object):
assert "===" not in s
assert "passed" not in s
def test_report_collectionfinish_hook(self, testdir):
@pytest.mark.parametrize(
"params", [(), ("--collect-only",)], ids=["no-params", "collect-only"]
)
def test_report_collectionfinish_hook(self, testdir, params):
testdir.makeconftest(
"""
def pytest_report_collectionfinish(config, startdir, items):
@ -664,7 +667,7 @@ class TestTerminalFunctional(object):
pass
"""
)
result = testdir.runpytest()
result = testdir.runpytest(*params)
result.stdout.fnmatch_lines(["collected 3 items", "hello from hook: 3 items"])