Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exceptions in subrequest finalizers #2496

Merged
merged 2 commits into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions _pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,10 +733,16 @@ def addfinalizer(self, finalizer):
self._finalizer.append(finalizer)

def finish(self):
exceptions = []
try:
while self._finalizer:
func = self._finalizer.pop()
func()
try:
func = self._finalizer.pop()
func()
except:
exceptions.append(sys.exc_info())
if exceptions:
py.builtin._reraise(*exceptions[0])
finally:
ihook = self._fixturemanager.session.ihook
ihook.pytest_fixture_post_finalizer(fixturedef=self)
Expand Down
1 change: 1 addition & 0 deletions changelog/2440.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Exceptions in a SubRequest's finish() block are suppressed until all finalizers are called, with the initial exception reraised.
26 changes: 26 additions & 0 deletions testing/python/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,32 @@ def test_second():
"*1 error*" # XXX the whole module collection fails
])

def test_request_subrequest_addfinalizer_exceptions(self, testdir):
testdir.makepyfile("""
import pytest
l = []
def _excepts():
raise Exception('Error')
@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)
@pytest.fixture
def excepts(subrequest):
subrequest.addfinalizer(_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)

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