From ff5317a7f38eecf0d38f3b1130f465d27722db69 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 14 Apr 2019 19:54:18 +0200 Subject: [PATCH] terminal: use pytest_collection_finish for reporting --- changelog/5113.bugfix.rst | 1 + src/_pytest/terminal.py | 6 ++---- testing/test_terminal.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 changelog/5113.bugfix.rst diff --git a/changelog/5113.bugfix.rst b/changelog/5113.bugfix.rst new file mode 100644 index 000000000..713b48967 --- /dev/null +++ b/changelog/5113.bugfix.rst @@ -0,0 +1 @@ +Deselected items from plugins using ``pytest_collect_modifyitems`` as a hookwrapper are correctly reported now. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 2d7132259..25c42c34c 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -553,10 +553,6 @@ class TerminalReporter(object): else: self.write_line(line) - @pytest.hookimpl(trylast=True) - def pytest_collection_modifyitems(self): - self.report_collect(True) - @pytest.hookimpl(trylast=True) def pytest_sessionstart(self, session): self._session = session @@ -609,6 +605,8 @@ class TerminalReporter(object): return result def pytest_collection_finish(self, session): + self.report_collect(True) + if self.config.getoption("collectonly"): self._printcollecteditems(session.items) diff --git a/testing/test_terminal.py b/testing/test_terminal.py index feacc242d..47becf00a 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -506,6 +506,37 @@ class TestTerminalFunctional(object): ) assert result.ret == 0 + def test_deselected_with_hookwrapper(self, testdir): + testpath = testdir.makeconftest( + """ + import pytest + + @pytest.hookimpl(hookwrapper=True) + def pytest_collection_modifyitems(config, items): + yield + deselected = items.pop() + config.hook.pytest_deselected(items=[deselected]) + """ + ) + testpath = testdir.makepyfile( + """ + def test_one(): + pass + def test_two(): + pass + def test_three(): + pass + """ + ) + result = testdir.runpytest(testpath) + result.stdout.fnmatch_lines( + [ + "collected 3 items / 1 deselected / 2 selected", + "*= 2 passed, 1 deselected in*", + ] + ) + assert result.ret == 0 + def test_show_deselected_items_using_markexpr_before_test_execution(self, testdir): testdir.makepyfile( test_show_deselected="""