Skip to content

Commit

Permalink
Fix --lf with explicit cmdline args
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Mar 31, 2020
1 parent 2d9dac9 commit bffbaa7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,17 @@ def pytest_make_collect_report(self, collector) -> Generator:
out = yield
res = out.get_result()

session = collector.session
lastfailed = self.lfplugin.lastfailed
filtered_result = [
x for x in res.result if x.nodeid in self.lfplugin.lastfailed
x
for x in res.result
if x.nodeid in lastfailed
# Include any passed arguments.
# This includes the whole module then (and deselects
# later), because it is not trivial / error-prone to match
# initial args to nodeids again.
or session.isinitpath(x.fspath)
]
if filtered_result:
res.result = filtered_result
Expand Down
60 changes: 55 additions & 5 deletions testing/test_cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest
from _pytest.config import ExitCode
from _pytest.pytester import Testdir

pytest_plugins = ("pytester",)

Expand Down Expand Up @@ -267,9 +268,9 @@ def test_3(): assert 0
result = testdir.runpytest(str(p), "--lf")
result.stdout.fnmatch_lines(
[
"collected 2 items",
"collected 3 items / 1 deselected / 2 selected",
"run-last-failure: rerun previous 2 failures",
"*= 2 passed in *",
"*= 2 passed, 1 deselected in *",
]
)
result = testdir.runpytest(str(p), "--lf")
Expand Down Expand Up @@ -345,7 +346,13 @@ def test_a2(): assert 1
result = testdir.runpytest("--lf", p2)
result.stdout.fnmatch_lines(["*1 passed*"])
result = testdir.runpytest("--lf", p)
result.stdout.fnmatch_lines(["collected 1 item", "*= 1 failed in *"])
result.stdout.fnmatch_lines(
[
"collected 2 items / 1 deselected / 1 selected",
"run-last-failure: rerun previous 1 failure",
"*= 1 failed, 1 deselected in *",
]
)

def test_lastfailed_usecase_splice(self, testdir, monkeypatch):
monkeypatch.setattr("sys.dont_write_bytecode", True)
Expand Down Expand Up @@ -690,9 +697,9 @@ def test_foo_4(): pass
result = testdir.runpytest(test_foo, "--last-failed")
result.stdout.fnmatch_lines(
[
"collected 1 item",
"collected 2 items / 1 deselected / 1 selected",
"run-last-failure: rerun previous 1 failure",
"*= 1 passed in *",
"*= 1 passed, 1 deselected in *",
]
)
assert self.get_cached_last_failed(testdir) == []
Expand Down Expand Up @@ -852,6 +859,49 @@ def test_lastfailed_with_known_failures_not_being_selected(self, testdir):
]
)

def test_lastfailed_args_with_deselected(self, testdir: Testdir) -> None:
"""Test regression with --lf running into NoMatch error.
This was caused by it not collecting (non-failed) nodes given as
arguments.
"""
testdir.makepyfile(
**{
"pkg1/test_1.py": """
def test_pass(): pass
def test_fail(): assert 0
""",
}
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["collected 2 items", "* 1 failed, 1 passed in *"])
assert result.ret == 1

result = testdir.runpytest("pkg1/test_1.py::test_pass", "--lf", "--co")
assert result.ret == 0
result.stdout.fnmatch_lines(
[
'*collected 1 item',
'<Module pkg1/test_1.py>',
' <Function test_pass>',
'run-last-failure: 1 known failures not in selected tests',
]
)

result = testdir.runpytest(
"pkg1/test_1.py::test_pass", "pkg1/test_1.py::test_fail", "--lf", "--co"
)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
'collected 2 items / 1 deselected / 1 selected',
'<Module pkg1/test_1.py>',
' <Function test_fail>',
'run-last-failure: rerun previous 1 failure',
'*= 1 deselected in *',
]
)


class TestNewFirst:
def test_newfirst_usecase(self, testdir):
Expand Down

0 comments on commit bffbaa7

Please sign in to comment.