From 6686c67a411dad3ad646d271265332d094f1980a Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Thu, 21 Nov 2013 01:15:24 +0000 Subject: [PATCH] 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. --- _pytest/runner.py | 9 +++++---- testing/test_runner.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/_pytest/runner.py b/_pytest/runner.py index 345080db0..3a6af4abc 100644 --- a/_pytest/runner.py +++ b/_pytest/runner.py @@ -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) diff --git a/testing/test_runner.py b/testing/test_runner.py index 8a6f4f839..4ca45ec40 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -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):