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.
This commit is contained in:
parent
80c223474c
commit
f674f6da9f
|
@ -509,9 +509,9 @@ def pytest_runtest_teardown(item: "Item", nextitem: Optional["Item"]) -> None:
|
||||||
|
|
||||||
:param nextitem:
|
:param nextitem:
|
||||||
The scheduled-to-be-next test item (None if no further test item is
|
The scheduled-to-be-next test item (None if no further test item is
|
||||||
scheduled). This argument can be used to perform exact teardowns,
|
scheduled). This argument is used to perform exact teardowns, i.e.
|
||||||
i.e. calling just enough finalizers so that nextitem only needs to
|
calling just enough finalizers so that nextitem only needs to call
|
||||||
call setup-functions.
|
setup functions.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -479,15 +479,18 @@ class SetupState:
|
||||||
|
|
||||||
def prepare(self, item: Item) -> None:
|
def prepare(self, item: Item) -> None:
|
||||||
"""Setup objects along the collector chain to the item."""
|
"""Setup objects along the collector chain to the item."""
|
||||||
|
needed_collectors = item.listchain()
|
||||||
|
|
||||||
# If a collector fails its setup, fail its entire subtree of items.
|
# If a collector fails its setup, fail its entire subtree of items.
|
||||||
# The setup is not retried for each item - the same exception is used.
|
# The setup is not retried for each item - the same exception is used.
|
||||||
for col, (finalizers, prepare_exc) in self.stack.items():
|
for col, (finalizers, prepare_exc) in self.stack.items():
|
||||||
|
assert col in needed_collectors, "previous item was not torn down properly"
|
||||||
if prepare_exc:
|
if prepare_exc:
|
||||||
raise prepare_exc
|
raise prepare_exc
|
||||||
|
|
||||||
needed_collectors = item.listchain()
|
|
||||||
for col in needed_collectors[len(self.stack) :]:
|
for col in needed_collectors[len(self.stack) :]:
|
||||||
assert col not in self.stack
|
assert col not in self.stack
|
||||||
|
# Push onto the stack.
|
||||||
self.stack[col] = ([col.teardown], None)
|
self.stack[col] = ([col.teardown], None)
|
||||||
try:
|
try:
|
||||||
col.setup()
|
col.setup()
|
||||||
|
|
Loading…
Reference in New Issue