Skip to content

Commit 9237b5f

Browse files
committed
Merge pull request #7915 from bluetech/fix-lf-package
cacheprovider: fix some files in packages getting lost from --lf (cherry picked from commit a66b6b8) with some needed adjustments to 6.1.x.
1 parent 95fd566 commit 9237b5f

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

changelog/7758.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed an issue where some files in packages are getting lost from ``--lf`` even though they contain tests that failed. Regressed in pytest 5.4.0.

src/_pytest/cacheprovider.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from _pytest.fixtures import FixtureRequest
3030
from _pytest.main import Session
3131
from _pytest.python import Module
32+
from _pytest.python import Package
3233
from _pytest.reports import TestReport
3334

3435

@@ -233,7 +234,10 @@ def __init__(self, lfplugin: "LFPlugin") -> None:
233234
def pytest_make_collect_report(
234235
self, collector: nodes.Collector
235236
) -> Optional[CollectReport]:
236-
if isinstance(collector, Module):
237+
# Packages are Modules, but _last_failed_paths only contains
238+
# test-bearing paths and doesn't try to include the paths of their
239+
# packages, so don't filter them.
240+
if isinstance(collector, Module) and not isinstance(collector, Package):
237241
if Path(str(collector.fspath)) not in self.lfplugin._last_failed_paths:
238242
self.lfplugin._skipped_files += 1
239243

testing/test_cacheprovider.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,36 @@ def test_pass(): pass
984984
)
985985
assert result.ret == 0
986986

987+
def test_packages(self, testdir: Testdir) -> None:
988+
"""Regression test for #7758.
989+
990+
The particular issue here was that Package nodes were included in the
991+
filtering, being themselves Modules for the __init__.py, even if they
992+
had failed Modules in them.
993+
994+
The tests includes a test in an __init__.py file just to make sure the
995+
fix doesn't somehow regress that, it is not critical for the issue.
996+
"""
997+
testdir.makepyfile(
998+
**{
999+
"__init__.py": "",
1000+
"a/__init__.py": "def test_a_init(): assert False",
1001+
"a/test_one.py": "def test_1(): assert False",
1002+
"b/__init__.py": "",
1003+
"b/test_two.py": "def test_2(): assert False",
1004+
},
1005+
)
1006+
testdir.makeini(
1007+
"""
1008+
[pytest]
1009+
python_files = *.py
1010+
"""
1011+
)
1012+
result = testdir.runpytest()
1013+
result.assert_outcomes(failed=3)
1014+
result = testdir.runpytest("--lf")
1015+
result.assert_outcomes(failed=3)
1016+
9871017

9881018
class TestNewFirst:
9891019
def test_newfirst_usecase(self, testdir):

0 commit comments

Comments
 (0)