From 54af0f4c65f3d63210e661193f3cef2f727f246d Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 5 Feb 2019 21:29:30 -0200 Subject: [PATCH] Call pytest_report_collectionfinish hook when --collect-only is passed Fix #2895 --- changelog/2895.bugfix.rst | 1 + src/_pytest/terminal.py | 15 ++++++++------- testing/test_terminal.py | 7 +++++-- 3 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 changelog/2895.bugfix.rst diff --git a/changelog/2895.bugfix.rst b/changelog/2895.bugfix.rst new file mode 100644 index 00000000000..8e01e193cad --- /dev/null +++ b/changelog/2895.bugfix.rst @@ -0,0 +1 @@ +The ``pytest_report_collectionfinish`` hook now is also called with ``--collect-only``. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 03b0761f263..eb35577f1b5 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -574,19 +574,20 @@ def pytest_report_header(self, config): 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 () diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 71e49fb42b5..798e8c16a62 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -649,7 +649,10 @@ def test_more_quiet_reporting(self, testdir): 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 @@ def test(i): pass """ ) - result = testdir.runpytest() + result = testdir.runpytest(*params) result.stdout.fnmatch_lines(["collected 3 items", "hello from hook: 3 items"])