Skip to content

Commit ceb4f3f

Browse files
committed
fixup! Fix regression with --lf and non-selected failures
1 parent bf3b26b commit ceb4f3f

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

changelog/5333.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix regression with ``--lf`` not re-running all tests with known failures from non-selected tests.

src/_pytest/cacheprovider.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def last_failed_paths(self):
168168
if result is None:
169169
rootpath = Path(self.config.rootdir)
170170
result = {rootpath / nodeid.split("::")[0] for nodeid in self.lastfailed}
171+
result = {x for x in result if x.exists()}
171172
self._last_failed_paths = result
172173
return result
173174

@@ -176,17 +177,13 @@ def pytest_ignore_collect(self, path):
176177
Ignore this file path if we are in --lf mode and it is not in the list of
177178
previously failed files.
178179
"""
179-
if (
180-
self.active
181-
and self._previously_failed_count
182-
and self.config.getoption("lf")
183-
and path.isfile()
184-
and self.lastfailed
185-
):
186-
skip_it = Path(path) not in self.last_failed_paths()
187-
if skip_it:
188-
self._skipped_files += 1
189-
return skip_it
180+
if self.active and self.config.getoption("lf") and path.isfile():
181+
last_failed_paths = self.last_failed_paths()
182+
if last_failed_paths:
183+
skip_it = Path(path) not in self.last_failed_paths()
184+
if skip_it:
185+
self._skipped_files += 1
186+
return skip_it
190187

191188
def pytest_report_collectionfinish(self):
192189
if self.active and self.config.getoption("verbose") >= 0:

testing/test_cacheprovider.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ def test_3(): pass
832832
]
833833
)
834834

835-
def test_lastfailed_with_unknown_failure(self, testdir):
835+
def test_lastfailed_with_known_failures_not_being_selected(self, testdir):
836836
testdir.makepyfile(
837837
**{
838838
"pkg1/test_1.py": """def test_1(): assert 0""",
@@ -852,6 +852,28 @@ def test_lastfailed_with_unknown_failure(self, testdir):
852852
]
853853
)
854854

855+
# Recreate file with known failure.
856+
testdir.makepyfile(**{"pkg1/test_1.py": """def test_1(): assert 0"""})
857+
result = testdir.runpytest("--lf")
858+
result.stdout.fnmatch_lines(
859+
[
860+
"collected 1 item",
861+
"run-last-failure: rerun previous 1 failure (skipped 1 file)",
862+
"* 1 failed in *",
863+
]
864+
)
865+
866+
# Remove/rename test.
867+
testdir.makepyfile(**{"pkg1/test_1.py": """def test_renamed(): assert 0"""})
868+
result = testdir.runpytest("--lf")
869+
result.stdout.fnmatch_lines(
870+
[
871+
"collected 1 item",
872+
"run-last-failure: 1 known failures not in selected tests",
873+
"* 1 failed in *",
874+
]
875+
)
876+
855877

856878
class TestNewFirst(object):
857879
def test_newfirst_usecase(self, testdir):

0 commit comments

Comments
 (0)