@@ -445,9 +445,9 @@ def test_b2():
445445 result = testdir .runpytest ("--lf" )
446446 result .stdout .fnmatch_lines (
447447 [
448- "collected 4 items / 2 deselected / 2 selected " ,
449- "run-last-failure: rerun previous 2 failures" ,
450- "*2 failed, 2 deselected in*" ,
448+ "collected 2 items " ,
449+ "run-last-failure: rerun previous 2 failures (skipped 1 file) " ,
450+ "*2 failed in*" ,
451451 ]
452452 )
453453
@@ -718,7 +718,7 @@ def test_bar_2():
718718 assert self .get_cached_last_failed (testdir ) == ["test_foo.py::test_foo_4" ]
719719
720720 result = testdir .runpytest ("--last-failed" )
721- result .stdout .fnmatch_lines (["*1 failed, 3 deselected*" ])
721+ result .stdout .fnmatch_lines (["*1 failed, 1 deselected*" ])
722722 assert self .get_cached_last_failed (testdir ) == ["test_foo.py::test_foo_4" ]
723723
724724 # 3. fix test_foo_4, run only test_foo.py
@@ -779,6 +779,58 @@ def test_2():
779779 result = testdir .runpytest ("--lf" , "--cache-clear" , "--lfnf" , "none" )
780780 result .stdout .fnmatch_lines (["*2 desel*" ])
781781
782+ def test_lastfailed_skip_collection (self , testdir ):
783+ """
784+ Test --lf behavior regarding skipping collection of files that are not marked as
785+ failed in the cache (#5172).
786+ """
787+ testdir .makepyfile (
788+ ** {
789+ "pkg1/test_1.py" : """
790+ import pytest
791+
792+ @pytest.mark.parametrize('i', range(3))
793+ def test_1(i): pass
794+ """ ,
795+ "pkg2/test_2.py" : """
796+ import pytest
797+
798+ @pytest.mark.parametrize('i', range(5))
799+ def test_1(i):
800+ assert i not in (1, 3)
801+ """ ,
802+ }
803+ )
804+ # first run: collects 8 items (test_1: 3, test_2: 5)
805+ result = testdir .runpytest ()
806+ result .stdout .fnmatch_lines (["collected 8 items" , "*2 failed*6 passed*" ])
807+ # second run: collects only 5 items from test_2, because all tests from test_1 have passed
808+ result = testdir .runpytest ("--lf" )
809+ result .stdout .fnmatch_lines (
810+ [
811+ "collected 5 items / 3 deselected / 2 selected" ,
812+ "run-last-failure: rerun previous 2 failures (skipped 1 file)" ,
813+ "*2 failed*3 deselected*" ,
814+ ]
815+ )
816+
817+ # add another file and check if message is correct when skipping more than 1 file
818+ testdir .makepyfile (
819+ ** {
820+ "pkg1/test_3.py" : """
821+ def test_3(): pass
822+ """
823+ }
824+ )
825+ result = testdir .runpytest ("--lf" )
826+ result .stdout .fnmatch_lines (
827+ [
828+ "collected 5 items / 3 deselected / 2 selected" ,
829+ "run-last-failure: rerun previous 2 failures (skipped 2 files)" ,
830+ "*2 failed*3 deselected*" ,
831+ ]
832+ )
833+
782834
783835class TestNewFirst (object ):
784836 def test_newfirst_usecase (self , testdir ):
0 commit comments