Skip to content

Commit fb6dad6

Browse files
authored
terminal: use pytest_collection_finish for reporting (#5113)
terminal: use pytest_collection_finish for reporting
2 parents 766fc23 + ff5317a commit fb6dad6

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

changelog/5113.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deselected items from plugins using ``pytest_collect_modifyitems`` as a hookwrapper are correctly reported now.

src/_pytest/terminal.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,6 @@ def report_collect(self, final=False):
552552
else:
553553
self.write_line(line)
554554

555-
@pytest.hookimpl(trylast=True)
556-
def pytest_collection_modifyitems(self):
557-
self.report_collect(True)
558-
559555
@pytest.hookimpl(trylast=True)
560556
def pytest_sessionstart(self, session):
561557
self._session = session
@@ -608,6 +604,8 @@ def pytest_report_header(self, config):
608604
return result
609605

610606
def pytest_collection_finish(self, session):
607+
self.report_collect(True)
608+
611609
if self.config.getoption("collectonly"):
612610
self._printcollecteditems(session.items)
613611

testing/test_terminal.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,37 @@ def test_three():
506506
)
507507
assert result.ret == 0
508508

509+
def test_deselected_with_hookwrapper(self, testdir):
510+
testpath = testdir.makeconftest(
511+
"""
512+
import pytest
513+
514+
@pytest.hookimpl(hookwrapper=True)
515+
def pytest_collection_modifyitems(config, items):
516+
yield
517+
deselected = items.pop()
518+
config.hook.pytest_deselected(items=[deselected])
519+
"""
520+
)
521+
testpath = testdir.makepyfile(
522+
"""
523+
def test_one():
524+
pass
525+
def test_two():
526+
pass
527+
def test_three():
528+
pass
529+
"""
530+
)
531+
result = testdir.runpytest(testpath)
532+
result.stdout.fnmatch_lines(
533+
[
534+
"collected 3 items / 1 deselected / 2 selected",
535+
"*= 2 passed, 1 deselected in*",
536+
]
537+
)
538+
assert result.ret == 0
539+
509540
def test_show_deselected_items_using_markexpr_before_test_execution(self, testdir):
510541
testdir.makepyfile(
511542
test_show_deselected="""

0 commit comments

Comments
 (0)