diff options
author | Thomi Richards <thomir@gmail.com> | 2014-09-13 08:54:37 +1200 |
---|---|---|
committer | Thomi Richards <thomir@gmail.com> | 2014-09-13 08:54:37 +1200 |
commit | d29e29c05925b3ab9d5c434b2c14a9715bc93f50 (patch) | |
tree | b70e15eed34c58c9dc95117b1a8dec6c9395a36b | |
parent | 7a27dadce59b4d2059139adf8ad8823dabd5f864 (diff) | |
parent | 4b6d19adcbed329ef3e5600d030248d58d5a8000 (diff) | |
download | testtools-d29e29c05925b3ab9d5c434b2c14a9715bc93f50.tar.gz |
Merge pull request #105 from rbtcollins/bug-1368440
Fix masking of fixture.setUp errors.
-rw-r--r-- | NEWS | 7 | ||||
-rw-r--r-- | testtools/testcase.py | 14 | ||||
-rw-r--r-- | testtools/tests/test_fixturesupport.py | 27 |
3 files changed, 46 insertions, 2 deletions
@@ -7,6 +7,13 @@ Changes and improvements to testtools_, grouped by release. NEXT ~~~~ +Improvements +------------ + +* Exceptions in a ``fixture.getDetails`` method will no longer mask errors + raised from the same fixture's ``setUp`` method. + (Robert Collins, #1368440) + 1.0.0 ~~~~~ diff --git a/testtools/testcase.py b/testtools/testcase.py index a52f38f..b646f82 100644 --- a/testtools/testcase.py +++ b/testtools/testcase.py @@ -661,8 +661,18 @@ class TestCase(unittest.TestCase): try: fixture.setUp() except: - gather_details(fixture.getDetails(), self.getDetails()) - raise + exc_info = sys.exc_info() + try: + gather_details(fixture.getDetails(), self.getDetails()) + except: + # Report the setUp exception, then raise the error during + # gather_details. + self._report_traceback(exc_info) + raise + else: + # Gather_details worked, so raise the exception setUp + # encountered. + reraise(*exc_info) else: self.addCleanup(fixture.cleanUp) self.addCleanup( diff --git a/testtools/tests/test_fixturesupport.py b/testtools/tests/test_fixturesupport.py index 2ccd1e8..e309045 100644 --- a/testtools/tests/test_fixturesupport.py +++ b/testtools/tests/test_fixturesupport.py @@ -10,6 +10,7 @@ from testtools import ( content_type, ) from testtools.compat import _b, _u +from testtools.matchers import Contains from testtools.testresult.doubles import ( ExtendedTestResult, ) @@ -112,6 +113,32 @@ class TestFixtureSupport(TestCase): self.assertEqual(['content', 'traceback'], sorted(details)) self.assertEqual('foobar', ''.join(details['content'].iter_text())) + def test_useFixture_original_exception_raised_if_gather_details_fails(self): + # In bug #1368440 it was reported that when a fixture fails setUp + # and gather_details errors on it, then the original exception that + # failed is not reported. + class BrokenFixture(fixtures.Fixture): + def getDetails(self): + raise AttributeError("getDetails broke") + def setUp(self): + fixtures.Fixture.setUp(self) + raise Exception("setUp broke") + fixture = BrokenFixture() + class SimpleTest(TestCase): + def test_foo(self): + self.useFixture(fixture) + result = ExtendedTestResult() + SimpleTest('test_foo').run(result) + self.assertEqual('addError', result._events[-2][0]) + details = result._events[-2][2] + self.assertEqual(['traceback', 'traceback-1'], sorted(details)) + self.assertThat( + ''.join(details['traceback'].iter_text()), + Contains('setUp broke')) + self.assertThat( + ''.join(details['traceback-1'].iter_text()), + Contains('getDetails broke')) + def test_suite(): from unittest import TestLoader |