From bb3d43c9a6d16174a05058686b9460ceff911e5a Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 1 Jan 2021 13:10:43 +0200 Subject: [PATCH] runner: ensure item.teardown() is called even if a finalizer raised If one finalizer fails, all of the subsequent finalizers still run, so the `teardown()` method should behave the same. --- src/_pytest/runner.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index b25438c23..c221b42a0 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -446,6 +446,7 @@ class SetupState: try: colitem = self.stack.pop() finalizers = self._finalizers.pop(colitem) + finalizers.insert(0, colitem.teardown) inner_exc = None while finalizers: fin = finalizers.pop() @@ -456,11 +457,10 @@ class SetupState: # ideally all should be reported. if inner_exc is None: inner_exc = e - if inner_exc: - raise inner_exc - colitem.teardown() for colitem in self._finalizers: assert colitem in self.stack + if inner_exc: + raise inner_exc except TEST_OUTCOME as e: # XXX Only first exception will be seen by user, # ideally all should be reported.