Skip to content

Commit f674f6d

Browse files
committed
runner: add a safety assert to SetupState.prepare
This ensures that the teardown for the previous item was done properly for this (next) item, i.e. there are no leftover teardowns.
1 parent 80c2234 commit f674f6d

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/_pytest/hookspec.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,9 @@ def pytest_runtest_teardown(item: "Item", nextitem: Optional["Item"]) -> None:
509509
510510
:param nextitem:
511511
The scheduled-to-be-next test item (None if no further test item is
512-
scheduled). This argument can be used to perform exact teardowns,
513-
i.e. calling just enough finalizers so that nextitem only needs to
514-
call setup-functions.
512+
scheduled). This argument is used to perform exact teardowns, i.e.
513+
calling just enough finalizers so that nextitem only needs to call
514+
setup functions.
515515
"""
516516

517517

src/_pytest/runner.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,15 +479,18 @@ def __init__(self) -> None:
479479

480480
def prepare(self, item: Item) -> None:
481481
"""Setup objects along the collector chain to the item."""
482+
needed_collectors = item.listchain()
483+
482484
# If a collector fails its setup, fail its entire subtree of items.
483485
# The setup is not retried for each item - the same exception is used.
484486
for col, (finalizers, prepare_exc) in self.stack.items():
487+
assert col in needed_collectors, "previous item was not torn down properly"
485488
if prepare_exc:
486489
raise prepare_exc
487490

488-
needed_collectors = item.listchain()
489491
for col in needed_collectors[len(self.stack) :]:
490492
assert col not in self.stack
493+
# Push onto the stack.
491494
self.stack[col] = ([col.teardown], None)
492495
try:
493496
col.setup()

0 commit comments

Comments
 (0)