Re-raise the first exception instead of the last

This will make more sense if multiple fixtures depend on each other.
It would be better if all exceptions could be shown however.

Also depend on python 2.5+ exception hierarchy and use sys module
directly.
This commit is contained in:
Floris Bruynooghe 2013-11-21 01:15:24 +00:00
parent 72752165df
commit 6686c67a41
2 changed files with 18 additions and 4 deletions

View File

@ -333,10 +333,11 @@ class SetupState(object):
fin = finalizers.pop()
try:
fin()
except KeyboardInterrupt:
raise
except:
exc = py.std.sys.exc_info()
except Exception:
# XXX Only first exception will be seen by user,
# ideally all should be reported.
if not exc:
exc = sys.exc_info()
if exc:
py.builtin._reraise(*exc)

View File

@ -58,6 +58,19 @@ class TestSetupState:
assert err.value.args == ('oops',)
assert r == ['fin3', 'fin1']
def test_teardown_multiple_fail(self, testdir):
# Ensure the first exception is the one which is re-raised.
# Ideally both would be reported however.
def fin1(): raise Exception('oops1')
def fin2(): raise Exception('oops2')
item = testdir.getitem("def test_func(): pass")
ss = runner.SetupState()
ss.addfinalizer(fin1, item)
ss.addfinalizer(fin2, item)
with pytest.raises(Exception) as err:
ss._callfinalizers(item)
assert err.value.args == ('oops2',)
class BaseFunctionalTests:
def test_passfunction(self, testdir):