From 61d15a600e8878d67353e39ea09527664c56c9b2 Mon Sep 17 00:00:00 2001 From: Eduardo Schettino Date: Mon, 7 Feb 2022 14:31:44 +0800 Subject: [PATCH 1/3] python: Fix count of selected tests on terminal collection summary. If there were errors or skipped modules on collection, pytest would mistakenly subtract those from the selected count. --- changelog/9626.bugfix.rst | 3 +++ src/_pytest/terminal.py | 4 ++-- testing/test_cacheprovider.py | 2 +- testing/test_terminal.py | 27 +++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 changelog/9626.bugfix.rst diff --git a/changelog/9626.bugfix.rst b/changelog/9626.bugfix.rst new file mode 100644 index 00000000000..0364b3c1bfe --- /dev/null +++ b/changelog/9626.bugfix.rst @@ -0,0 +1,3 @@ +Fix count of selected tests on terminal collection summary. + +If there were errors or skipped modules on collection, pytest would mistakenly subtract those from the selected count. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 4bcc968d100..b4848c48aba 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -663,7 +663,7 @@ def report_collect(self, final: bool = False) -> None: errors = len(self.stats.get("error", [])) skipped = len(self.stats.get("skipped", [])) deselected = len(self.stats.get("deselected", [])) - selected = self._numcollected - errors - skipped - deselected + selected = self._numcollected - deselected line = "collected " if final else "collecting " line += ( str(self._numcollected) + " item" + ("" if self._numcollected == 1 else "s") @@ -674,7 +674,7 @@ def report_collect(self, final: bool = False) -> None: line += " / %d deselected" % deselected if skipped: line += " / %d skipped" % skipped - if self._numcollected > selected > 0: + if self._numcollected > selected: line += " / %d selected" % selected if self.isatty: self.rewrite(line, bold=True, erase=True) diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index cc6d547dfb1..2baa3c8f189 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -773,7 +773,7 @@ def pytest_sessionfinish(): result = pytester.runpytest("--lf", "--lfnf", "none") result.stdout.fnmatch_lines( [ - "collected 2 items / 2 deselected", + "collected 2 items / 2 deselected / 0 selected", "run-last-failure: no previously failed tests, deselecting all items.", "deselected=2", "* 2 deselected in *", diff --git a/testing/test_terminal.py b/testing/test_terminal.py index e5f41266280..0bba2378cf8 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -783,6 +783,33 @@ def test_pass(): result.stdout.no_fnmatch_line("*= 1 deselected =*") assert result.ret == 0 + def test_selected_count_with_error(self, pytester: Pytester) -> None: + testpath = pytester.makepyfile( + test_selected_count_3=""" + def test_one(): + pass + def test_two(): + pass + def test_three(): + pass + """, + test_selected_count_error=""" + 5/0 + def test_foo(): + pass + def test_bar(): + pass + """, + ) + result = pytester.runpytest("-k", "test_t") + result.stdout.fnmatch_lines( + [ + "collected 3 items / 1 error / 1 deselected / 2 selected", + "* ERROR collecting test_selected_count_error.py *", + ] + ) + assert result.ret == ExitCode.INTERRUPTED + def test_no_skip_summary_if_failure(self, pytester: Pytester) -> None: pytester.makepyfile( """ From 340528c1bffe1a9942133b4fb2617e1113c13f09 Mon Sep 17 00:00:00 2001 From: Eduardo Schettino Date: Tue, 8 Feb 2022 11:34:51 +0800 Subject: [PATCH 2/3] fix: lint. --- testing/test_terminal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 0bba2378cf8..f0e58e5b4c4 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -784,7 +784,7 @@ def test_pass(): assert result.ret == 0 def test_selected_count_with_error(self, pytester: Pytester) -> None: - testpath = pytester.makepyfile( + pytester.makepyfile( test_selected_count_3=""" def test_one(): pass From caa9b3496e133e4ee26311a53d65a36a8446a089 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 15 Feb 2022 07:59:14 -0300 Subject: [PATCH 3/3] Update 9626.bugfix.rst --- changelog/9626.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/9626.bugfix.rst b/changelog/9626.bugfix.rst index 0364b3c1bfe..44d3734a1d2 100644 --- a/changelog/9626.bugfix.rst +++ b/changelog/9626.bugfix.rst @@ -1,3 +1,3 @@ -Fix count of selected tests on terminal collection summary. +Fixed count of selected tests on terminal collection summary when there were errors or skipped modules. If there were errors or skipped modules on collection, pytest would mistakenly subtract those from the selected count.