diff options
author | Gavin Panella <gavin@gromper.net> | 2011-06-17 22:43:11 +0100 |
---|---|---|
committer | Gavin Panella <gavin@gromper.net> | 2011-06-17 22:43:11 +0100 |
commit | a6f79f5c8135e9b54fa963c23614d6be5404bb9e (patch) | |
tree | 0df275340749540c5007fd128651aff9fef860b8 | |
parent | 5432e4a9f4e4c0a66999f2aa6fd99223e8fe1431 (diff) | |
download | fixtures-a6f79f5c8135e9b54fa963c23614d6be5404bb9e.tar.gz |
Gather details from fixtures that fail to setUp() in TestWithFixtures.
-rw-r--r-- | lib/fixtures/testcase.py | 19 | ||||
-rw-r--r-- | lib/fixtures/tests/test_testcase.py | 54 |
2 files changed, 68 insertions, 5 deletions
diff --git a/lib/fixtures/testcase.py b/lib/fixtures/testcase.py index 791a30b..38fc4ff 100644 --- a/lib/fixtures/testcase.py +++ b/lib/fixtures/testcase.py @@ -19,6 +19,8 @@ __all__ = [ import unittest +from fixtures.fixture import gather_details + class TestWithFixtures(unittest.TestCase): """A TestCase with a helper function to use fixtures. @@ -35,6 +37,17 @@ class TestWithFixtures(unittest.TestCase): :return: The fixture, after setting it up and scheduling a cleanup for it. """ - fixture.setUp() - self.addCleanup(fixture.cleanUp) - return fixture + use_details = ( + gather_details is not None and + getattr(self, "addDetail", None) is not None) + try: + fixture.setUp() + except: + if use_details: + gather_details(fixture, self) + raise + else: + self.addCleanup(fixture.cleanUp) + if use_details: + self.addCleanup(gather_details, fixture, self) + return fixture diff --git a/lib/fixtures/tests/test_testcase.py b/lib/fixtures/tests/test_testcase.py index aa9682d..3f186c5 100644 --- a/lib/fixtures/tests/test_testcase.py +++ b/lib/fixtures/tests/test_testcase.py @@ -15,8 +15,12 @@ import unittest import testtools +from testtools.content import text_content +from testtools.testcase import skipIf import fixtures +from fixtures import TestWithFixtures +from fixtures.fixture import gather_details from fixtures.tests.helpers import LoggingFixture @@ -24,7 +28,7 @@ class TestTestWithFixtures(unittest.TestCase): def test_useFixture(self): fixture = LoggingFixture() - class SimpleTest(testtools.TestCase, fixtures.TestWithFixtures): + class SimpleTest(testtools.TestCase, TestWithFixtures): def test_foo(self): self.useFixture(fixture) result = unittest.TestResult() @@ -38,10 +42,56 @@ class TestTestWithFixtures(unittest.TestCase): calls.append('called') raise Exception('foo') fixture = fixtures.FunctionFixture(lambda:None, raiser) - class SimpleTest(testtools.TestCase, fixtures.TestWithFixtures): + class SimpleTest(testtools.TestCase, TestWithFixtures): def test_foo(self): self.useFixture(fixture) result = unittest.TestResult() SimpleTest('test_foo').run(result) self.assertFalse(result.wasSuccessful()) self.assertEqual(['called'], calls) + + @skipIf(gather_details is None, "gather_details() is not available.") + def test_useFixture_details_captured_from_setUp(self): + # Details added during fixture set-up are gathered even if setUp() + # fails with an exception. + class SomethingBroke(Exception): pass + class BrokenFixture(fixtures.Fixture): + def setUp(self): + super(BrokenFixture, self).setUp() + self.addDetail('content', text_content("foobar")) + raise SomethingBroke() + broken_fixture = BrokenFixture() + class DetailedTestCase(TestWithFixtures, testtools.TestCase): + def setUp(self): + super(DetailedTestCase, self).setUp() + self.useFixture(broken_fixture) + def test(self): + pass + detailed_test_case = DetailedTestCase("test") + self.assertRaises(SomethingBroke, detailed_test_case.setUp) + self.assertEqual( + {"content": text_content("foobar")}, + broken_fixture.getDetails()) + self.assertEqual( + {"content": text_content("foobar")}, + detailed_test_case.getDetails()) + + @skipIf(gather_details is None, "gather_details() is not available.") + def test_useFixture_details_not_captured_from_setUp(self): + # Details added during fixture set-up are not gathered if the test + # case does not have the ability to accept those details. + class SomethingBroke(Exception): pass + class BrokenFixture(fixtures.Fixture): + def setUp(self): + super(BrokenFixture, self).setUp() + self.addDetail('content', text_content("foobar")) + raise SomethingBroke() + broken_fixture = BrokenFixture() + class NonDetailedTestCase(TestWithFixtures, unittest.TestCase): + def setUp(self): + super(NonDetailedTestCase, self).setUp() + self.useFixture(broken_fixture) + def test(self): + pass + non_detailed_test_case = NonDetailedTestCase("test") + self.assertRaises(SomethingBroke, non_detailed_test_case.setUp) |