Improve test to ensure the expected function is re-raised
This commit is contained in:
parent
5a856b6e29
commit
4e4ebbef5a
|
@ -742,7 +742,10 @@ class FixtureDef:
|
||||||
except:
|
except:
|
||||||
exceptions.append(sys.exc_info())
|
exceptions.append(sys.exc_info())
|
||||||
if exceptions:
|
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:
|
finally:
|
||||||
ihook = self._fixturemanager.session.ihook
|
ihook = self._fixturemanager.session.ihook
|
||||||
ihook.pytest_fixture_post_finalizer(fixturedef=self)
|
ihook.pytest_fixture_post_finalizer(fixturedef=self)
|
||||||
|
|
|
@ -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.
|
||||||
|
|
|
@ -658,11 +658,15 @@ class TestRequestBasic(object):
|
||||||
])
|
])
|
||||||
|
|
||||||
def test_request_subrequest_addfinalizer_exceptions(self, testdir):
|
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("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
l = []
|
l = []
|
||||||
def _excepts():
|
def _excepts(where):
|
||||||
raise Exception('Error')
|
raise Exception('Error in %s fixture' % where)
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def subrequest(request):
|
def subrequest(request):
|
||||||
return request
|
return request
|
||||||
|
@ -670,18 +674,21 @@ class TestRequestBasic(object):
|
||||||
def something(subrequest):
|
def something(subrequest):
|
||||||
subrequest.addfinalizer(lambda: l.append(1))
|
subrequest.addfinalizer(lambda: l.append(1))
|
||||||
subrequest.addfinalizer(lambda: l.append(2))
|
subrequest.addfinalizer(lambda: l.append(2))
|
||||||
subrequest.addfinalizer(_excepts)
|
subrequest.addfinalizer(lambda: _excepts('something'))
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def excepts(subrequest):
|
def excepts(subrequest):
|
||||||
subrequest.addfinalizer(_excepts)
|
subrequest.addfinalizer(lambda: _excepts('excepts'))
|
||||||
subrequest.addfinalizer(lambda: l.append(3))
|
subrequest.addfinalizer(lambda: l.append(3))
|
||||||
def test_first(something, excepts):
|
def test_first(something, excepts):
|
||||||
pass
|
pass
|
||||||
def test_second():
|
def test_second():
|
||||||
assert l == [3, 2, 1]
|
assert l == [3, 2, 1]
|
||||||
""")
|
""")
|
||||||
reprec = testdir.inline_run()
|
result = testdir.runpytest()
|
||||||
reprec.assertoutcome(passed=2, failed=1)
|
result.stdout.fnmatch_lines([
|
||||||
|
'*Exception: Error in excepts fixture',
|
||||||
|
'* 2 passed, 1 error in *',
|
||||||
|
])
|
||||||
|
|
||||||
def test_request_getmodulepath(self, testdir):
|
def test_request_getmodulepath(self, testdir):
|
||||||
modcol = testdir.getmodulecol("def test_somefunc(): pass")
|
modcol = testdir.getmodulecol("def test_somefunc(): pass")
|
||||||
|
|
Loading…
Reference in New Issue