From bb878a2b13508e88ace7718759065de56a63aac5 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sun, 14 Jun 2020 13:35:59 +0300 Subject: [PATCH] runner: don't try to teardown previous items from pytest_runtest_setup While working on improving the documentation of the `pytest_runtest_setup` hook, I came up with this text: > Called to perform the setup phase of the test item. > > The default implementation runs ``setup()`` on item and all of its > parents (which haven't been setup yet). This includes obtaining the > values of fixtures required by the item (which haven't been obtained > yet). But upon closer inspection I noticed this line at the start of `SetupState.prepare` (which is what does the actual work for `pytest_runtest_setup`): self._teardown_towards(needed_collectors) which implies that the setup phase of one item might trigger teardowns of *previous* items. This complicates the simple explanation. It also seems like a completely undesirable thing to do, because it breaks isolation between tests -- e.g. a failed teardown of one item shouldn't cause the failure of some other items just because it happens to run after it. So the first thing I tried was to remove that line and see if anything breaks -- nothing did. At least pytest's own test suite runs fine. So maybe it's just dead code? --- src/_pytest/runner.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index 3ca8d7ea4..b6c89dce5 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -409,16 +409,15 @@ class SetupState: raise exc def prepare(self, colitem) -> None: - """ setup objects along the collector chain to the test-method - and teardown previously setup objects.""" - needed_collectors = colitem.listchain() - self._teardown_towards(needed_collectors) + """Setup objects along the collector chain to the test-method.""" # check if the last collection node has raised an error for col in self.stack: if hasattr(col, "_prepare_exc"): exc = col._prepare_exc # type: ignore[attr-defined] # noqa: F821 raise exc + + needed_collectors = colitem.listchain() for col in needed_collectors[len(self.stack) :]: self.stack.append(col) try: