summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRan Benita <ran@unusedvar.com>2021-01-31 01:49:25 +0200
committerRan Benita <ran@unusedvar.com>2021-02-06 20:46:08 +0200
commitf674f6da9fec826bc8c4e08781e7309322fc12cc (patch)
tree10fdb72013df25f8e89f5c8512511f858a7a208f /src
parent80c223474c98fd59a07776994e672e934866c7d5 (diff)
downloadpytest-f674f6da9fec826bc8c4e08781e7309322fc12cc.tar.gz
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.
Diffstat (limited to 'src')
-rw-r--r--src/_pytest/hookspec.py6
-rw-r--r--src/_pytest/runner.py5
2 files changed, 7 insertions, 4 deletions
diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py
index 41c12a2cc..b0b8fd53d 100644
--- a/src/_pytest/hookspec.py
+++ b/src/_pytest/hookspec.py
@@ -509,9 +509,9 @@ def pytest_runtest_teardown(item: "Item", nextitem: Optional["Item"]) -> None:
:param nextitem:
The scheduled-to-be-next test item (None if no further test item is
- scheduled). This argument can be used to perform exact teardowns,
- i.e. calling just enough finalizers so that nextitem only needs to
- call setup-functions.
+ scheduled). This argument is used to perform exact teardowns, i.e.
+ calling just enough finalizers so that nextitem only needs to call
+ setup functions.
"""
diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py
index ae76a2472..124cf531a 100644
--- a/src/_pytest/runner.py
+++ b/src/_pytest/runner.py
@@ -479,15 +479,18 @@ class SetupState:
def prepare(self, item: Item) -> None:
"""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.
# The setup is not retried for each item - the same exception is used.
for col, (finalizers, prepare_exc) in self.stack.items():
+ assert col in needed_collectors, "previous item was not torn down properly"
if prepare_exc:
raise prepare_exc
- needed_collectors = item.listchain()
for col in needed_collectors[len(self.stack) :]:
assert col not in self.stack
+ # Push onto the stack.
self.stack[col] = ([col.teardown], None)
try:
col.setup()