Skip to content

Commit c9cf2d4

Browse files
authored
Fix count of selected tests on terminal collection summary (#9628)
1 parent f224517 commit c9cf2d4

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

changelog/9626.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed count of selected tests on terminal collection summary when there were errors or skipped modules.
2+
3+
If there were errors or skipped modules on collection, pytest would mistakenly subtract those from the selected count.

src/_pytest/terminal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ def report_collect(self, final: bool = False) -> None:
663663
errors = len(self.stats.get("error", []))
664664
skipped = len(self.stats.get("skipped", []))
665665
deselected = len(self.stats.get("deselected", []))
666-
selected = self._numcollected - errors - skipped - deselected
666+
selected = self._numcollected - deselected
667667
line = "collected " if final else "collecting "
668668
line += (
669669
str(self._numcollected) + " item" + ("" if self._numcollected == 1 else "s")
@@ -674,7 +674,7 @@ def report_collect(self, final: bool = False) -> None:
674674
line += " / %d deselected" % deselected
675675
if skipped:
676676
line += " / %d skipped" % skipped
677-
if self._numcollected > selected > 0:
677+
if self._numcollected > selected:
678678
line += " / %d selected" % selected
679679
if self.isatty:
680680
self.rewrite(line, bold=True, erase=True)

testing/test_cacheprovider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ def pytest_sessionfinish():
773773
result = pytester.runpytest("--lf", "--lfnf", "none")
774774
result.stdout.fnmatch_lines(
775775
[
776-
"collected 2 items / 2 deselected",
776+
"collected 2 items / 2 deselected / 0 selected",
777777
"run-last-failure: no previously failed tests, deselecting all items.",
778778
"deselected=2",
779779
"* 2 deselected in *",

testing/test_terminal.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,33 @@ def test_pass():
783783
result.stdout.no_fnmatch_line("*= 1 deselected =*")
784784
assert result.ret == 0
785785

786+
def test_selected_count_with_error(self, pytester: Pytester) -> None:
787+
pytester.makepyfile(
788+
test_selected_count_3="""
789+
def test_one():
790+
pass
791+
def test_two():
792+
pass
793+
def test_three():
794+
pass
795+
""",
796+
test_selected_count_error="""
797+
5/0
798+
def test_foo():
799+
pass
800+
def test_bar():
801+
pass
802+
""",
803+
)
804+
result = pytester.runpytest("-k", "test_t")
805+
result.stdout.fnmatch_lines(
806+
[
807+
"collected 3 items / 1 error / 1 deselected / 2 selected",
808+
"* ERROR collecting test_selected_count_error.py *",
809+
]
810+
)
811+
assert result.ret == ExitCode.INTERRUPTED
812+
786813
def test_no_skip_summary_if_failure(self, pytester: Pytester) -> None:
787814
pytester.makepyfile(
788815
"""

0 commit comments

Comments
 (0)