Skip to content

Commit ff5317a

Browse files
committed
terminal: use pytest_collection_finish for reporting
1 parent 19035f4 commit ff5317a

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
@@ -553,10 +553,6 @@ def report_collect(self, final=False):
553553
else:
554554
self.write_line(line)
555555

556-
@pytest.hookimpl(trylast=True)
557-
def pytest_collection_modifyitems(self):
558-
self.report_collect(True)
559-
560556
@pytest.hookimpl(trylast=True)
561557
def pytest_sessionstart(self, session):
562558
self._session = session
@@ -609,6 +605,8 @@ def pytest_report_header(self, config):
609605
return result
610606

611607
def pytest_collection_finish(self, session):
608+
self.report_collect(True)
609+
612610
if self.config.getoption("collectonly"):
613611
self._printcollecteditems(session.items)
614612

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)