Skip to content

Commit 73963aa

Browse files
committed
cache: NFPlugin: keep known nodeids
Caveat: does not forget about old nodeids Fixes pytest-dev#5206
1 parent c7eeb05 commit 73963aa

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

changelog/5206.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``--nf`` to not forget about known nodeids with partial test selection.

src/_pytest/cacheprovider.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ def __init__(self, config):
264264
self.cached_nodeids = config.cache.get("cache/nodeids", [])
265265

266266
def pytest_collection_modifyitems(self, session, config, items):
267+
new_items = OrderedDict()
267268
if self.active:
268-
new_items = OrderedDict()
269269
other_items = OrderedDict()
270270
for item in items:
271271
if item.nodeid not in self.cached_nodeids:
@@ -276,7 +276,11 @@ def pytest_collection_modifyitems(self, session, config, items):
276276
items[:] = self._get_increasing_order(
277277
new_items.values()
278278
) + self._get_increasing_order(other_items.values())
279-
self.cached_nodeids = [x.nodeid for x in items if isinstance(x, pytest.Item)]
279+
else:
280+
for item in items:
281+
if item.nodeid not in self.cached_nodeids:
282+
new_items[item.nodeid] = item
283+
self.cached_nodeids.extend(new_items)
280284

281285
def _get_increasing_order(self, items):
282286
return sorted(items, key=lambda item: item.fspath.mtime(), reverse=True)

testing/test_cacheprovider.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,8 +982,13 @@ def test_1(num): assert num
982982
)
983983
testdir.tmpdir.join("test_1/test_1.py").setmtime(1)
984984

985-
result = testdir.runpytest("-v", "--nf")
985+
# Running only a subset does not forget about existing ones.
986+
result = testdir.runpytest("-v", "--nf", "test_2/test_2.py")
987+
result.stdout.fnmatch_lines(
988+
["*test_2/test_2.py::test_1[1*", "*test_2/test_2.py::test_1[2*"]
989+
)
986990

991+
result = testdir.runpytest("-v", "--nf")
987992
result.stdout.fnmatch_lines(
988993
[
989994
"*test_1/test_1.py::test_1[3*",

0 commit comments

Comments
 (0)