Skip to content

Commit 4e4ebbe

Browse files
committed
Improve test to ensure the expected function is re-raised
1 parent 5a856b6 commit 4e4ebbe

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

_pytest/fixtures.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,10 @@ def finish(self):
742742
except:
743743
exceptions.append(sys.exc_info())
744744
if exceptions:
745-
py.builtin._reraise(*exceptions[0])
745+
e = exceptions[0]
746+
del exceptions # ensure we don't keep all frames alive because of the traceback
747+
py.builtin._reraise(*e)
748+
746749
finally:
747750
ihook = self._fixturemanager.session.ihook
748751
ihook.pytest_fixture_post_finalizer(fixturedef=self)

changelog/2440.bugfix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Exceptions in a SubRequest's finish() block are suppressed until all finalizers are called, with the initial exception reraised.
1+
Exceptions raised during teardown by finalizers are now suppressed until all finalizers are called, with the initial exception reraised.

testing/python/fixture.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -658,30 +658,37 @@ def test_second():
658658
])
659659

660660
def test_request_subrequest_addfinalizer_exceptions(self, testdir):
661+
"""
662+
Ensure exceptions raised during teardown by a finalizer are suppressed
663+
until all finalizers are called, re-raising the first exception (#2440)
664+
"""
661665
testdir.makepyfile("""
662666
import pytest
663667
l = []
664-
def _excepts():
665-
raise Exception('Error')
668+
def _excepts(where):
669+
raise Exception('Error in %s fixture' % where)
666670
@pytest.fixture
667671
def subrequest(request):
668672
return request
669673
@pytest.fixture
670674
def something(subrequest):
671675
subrequest.addfinalizer(lambda: l.append(1))
672676
subrequest.addfinalizer(lambda: l.append(2))
673-
subrequest.addfinalizer(_excepts)
677+
subrequest.addfinalizer(lambda: _excepts('something'))
674678
@pytest.fixture
675679
def excepts(subrequest):
676-
subrequest.addfinalizer(_excepts)
680+
subrequest.addfinalizer(lambda: _excepts('excepts'))
677681
subrequest.addfinalizer(lambda: l.append(3))
678682
def test_first(something, excepts):
679683
pass
680684
def test_second():
681685
assert l == [3, 2, 1]
682686
""")
683-
reprec = testdir.inline_run()
684-
reprec.assertoutcome(passed=2, failed=1)
687+
result = testdir.runpytest()
688+
result.stdout.fnmatch_lines([
689+
'*Exception: Error in excepts fixture',
690+
'* 2 passed, 1 error in *',
691+
])
685692

686693
def test_request_getmodulepath(self, testdir):
687694
modcol = testdir.getmodulecol("def test_somefunc(): pass")

0 commit comments

Comments
 (0)