From 2b14edb108f32c49c15b5e0c0ef4d27880b09e0f Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 1 Jan 2021 12:08:12 +0200 Subject: [PATCH] runner: express SetupState.teardown_all() in terms of teardown_exact() and remove it Makes it easier to understand with fewer methods. --- src/_pytest/runner.py | 13 +++++-------- testing/python/fixtures.py | 2 +- testing/test_runner.py | 13 +++++++------ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index 5d35f520a..525aafc76 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -105,7 +105,7 @@ def pytest_sessionstart(session: "Session") -> None: def pytest_sessionfinish(session: "Session") -> None: - session._setupstate.teardown_all() + session._setupstate.teardown_exact(None) def pytest_runtest_protocol(item: Item, nextitem: Optional[Item]) -> bool: @@ -177,7 +177,7 @@ def pytest_runtest_call(item: Item) -> None: def pytest_runtest_teardown(item: Item, nextitem: Optional[Item]) -> None: _update_current_test_var(item, "teardown") - item.session._setupstate.teardown_exact(item, nextitem) + item.session._setupstate.teardown_exact(nextitem) _update_current_test_var(item, None) @@ -455,7 +455,7 @@ class SetupState: for colitem in self._finalizers: assert colitem in self.stack - def teardown_exact(self, item: Item, nextitem: Optional[Item]) -> None: + def teardown_exact(self, nextitem: Optional[Item]) -> None: needed_collectors = nextitem and nextitem.listchain() or [] exc = None while self.stack: @@ -470,11 +470,8 @@ class SetupState: exc = e if exc: raise exc - - def teardown_all(self) -> None: - while self.stack: - self._pop_and_teardown() - assert not self._finalizers + if nextitem is None: + assert not self._finalizers def collect_one_node(collector: Collector) -> CollectReport: diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 12340e690..d12973396 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -856,7 +856,7 @@ class TestRequestBasic: teardownlist = parent.obj.teardownlist ss = item.session._setupstate assert not teardownlist - ss.teardown_exact(item, None) + ss.teardown_exact(None) print(ss.stack) assert teardownlist == [1] diff --git a/testing/test_runner.py b/testing/test_runner.py index 20c81a62f..aca1bd7ce 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -34,9 +34,10 @@ class TestSetupState: def test_teardown_exact_stack_empty(self, pytester: Pytester) -> None: item = pytester.getitem("def test_func(): pass") ss = runner.SetupState() - ss.teardown_exact(item, None) - ss.teardown_exact(item, None) - ss.teardown_exact(item, None) + ss.prepare(item) + ss.teardown_exact(None) + ss.teardown_exact(None) + ss.teardown_exact(None) def test_setup_fails_and_failure_is_cached(self, pytester: Pytester) -> None: item = pytester.getitem( @@ -69,7 +70,7 @@ class TestSetupState: ss.addfinalizer(fin2, item) ss.addfinalizer(fin3, item) with pytest.raises(Exception) as err: - ss.teardown_exact(item, None) + ss.teardown_exact(None) assert err.value.args == ("oops",) assert r == ["fin3", "fin1"] @@ -88,7 +89,7 @@ class TestSetupState: ss.addfinalizer(fin1, item) ss.addfinalizer(fin2, item) with pytest.raises(Exception) as err: - ss.teardown_exact(item, None) + ss.teardown_exact(None) assert err.value.args == ("oops2",) def test_teardown_multiple_scopes_one_fails(self, pytester: Pytester) -> None: @@ -106,7 +107,7 @@ class TestSetupState: ss.addfinalizer(fin_func, item) ss.prepare(item) with pytest.raises(Exception, match="oops1"): - ss.teardown_exact(item, None) + ss.teardown_exact(None) assert module_teardown