Skip to content

Commit

Permalink
Improve test to ensure the expected function is re-raised
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jun 13, 2017
1 parent 5a856b6 commit 4e4ebbe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
5 changes: 4 additions & 1 deletion _pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,10 @@ def finish(self):
except:
exceptions.append(sys.exc_info())
if exceptions:
py.builtin._reraise(*exceptions[0])
e = exceptions[0]
del exceptions # ensure we don't keep all frames alive because of the traceback
py.builtin._reraise(*e)

finally:
ihook = self._fixturemanager.session.ihook
ihook.pytest_fixture_post_finalizer(fixturedef=self)
Expand Down
2 changes: 1 addition & 1 deletion changelog/2440.bugfix
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Exceptions in a SubRequest's finish() block are suppressed until all finalizers are called, with the initial exception reraised.
Exceptions raised during teardown by finalizers are now suppressed until all finalizers are called, with the initial exception reraised.
19 changes: 13 additions & 6 deletions testing/python/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,30 +658,37 @@ def test_second():
])

def test_request_subrequest_addfinalizer_exceptions(self, testdir):
"""
Ensure exceptions raised during teardown by a finalizer are suppressed
until all finalizers are called, re-raising the first exception (#2440)
"""
testdir.makepyfile("""
import pytest
l = []
def _excepts():
raise Exception('Error')
def _excepts(where):
raise Exception('Error in %s fixture' % where)
@pytest.fixture
def subrequest(request):
return request
@pytest.fixture
def something(subrequest):
subrequest.addfinalizer(lambda: l.append(1))
subrequest.addfinalizer(lambda: l.append(2))
subrequest.addfinalizer(_excepts)
subrequest.addfinalizer(lambda: _excepts('something'))
@pytest.fixture
def excepts(subrequest):
subrequest.addfinalizer(_excepts)
subrequest.addfinalizer(lambda: _excepts('excepts'))
subrequest.addfinalizer(lambda: l.append(3))
def test_first(something, excepts):
pass
def test_second():
assert l == [3, 2, 1]
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=2, failed=1)
result = testdir.runpytest()
result.stdout.fnmatch_lines([
'*Exception: Error in excepts fixture',
'* 2 passed, 1 error in *',
])

def test_request_getmodulepath(self, testdir):
modcol = testdir.getmodulecol("def test_somefunc(): pass")
Expand Down

0 comments on commit 4e4ebbe

Please sign in to comment.