Merge lp:~lifeless/testtools/fixtures into lp:~testtools-committers/testtools/trunk

Proposed by Robert Collins
Status: Merged
Approved by: Jonathan Lange
Approved revision: 122
Merged at revision: 124
Proposed branch: lp:~lifeless/testtools/fixtures
Merge into: lp:~testtools-committers/testtools/trunk
Diff against target: 184 lines (+108/-2)
5 files modified
MANUAL (+10/-0)
NEWS (+3/-0)
README (+9/-2)
testtools/testcase.py (+29/-0)
testtools/tests/test_testtools.py (+57/-0)
To merge this branch: bzr merge lp:~lifeless/testtools/fixtures
Reviewer Review Type Date Requested Status
Jonathan Lange Approve
Review via email: mp+39388@code.launchpad.net

Description of the change

useFixture. Woo.

To post a comment you must log in.
Revision history for this message
Jonathan Lange (jml) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'MANUAL'
--- MANUAL 2010-10-17 16:34:55 +0000
+++ MANUAL 2010-10-26 16:56:50 +0000
@@ -116,6 +116,16 @@
116``skipTest`` support, the ``skip`` name is now deprecated (but no warning116``skipTest`` support, the ``skip`` name is now deprecated (but no warning
117is emitted yet - some time in the future we may do so).117is emitted yet - some time in the future we may do so).
118118
119TestCase.useFixture
120~~~~~~~~~~~~~~~~~~~
121
122``useFixture(fixture)`` calls setUp on the fixture, schedules a cleanup to
123clean it up, and schedules a cleanup to attach all details held by the
124fixture to the details dict of the test case. The fixture object should meet
125the ``fixtures.Fixture`` protocol (version 0.3.4 or newer). This is useful
126for moving code out of setUp and tearDown methods and into composable side
127classes.
128
119129
120New assertion methods130New assertion methods
121~~~~~~~~~~~~~~~~~~~~~131~~~~~~~~~~~~~~~~~~~~~
122132
=== modified file 'NEWS'
--- NEWS 2010-10-25 22:10:37 +0000
+++ NEWS 2010-10-26 16:56:50 +0000
@@ -24,6 +24,9 @@
24* addUnexpectedSuccess is translated to addFailure for test results that don't24* addUnexpectedSuccess is translated to addFailure for test results that don't
25 know about addUnexpectedSuccess. (Jonathan Lange, #654474)25 know about addUnexpectedSuccess. (Jonathan Lange, #654474)
2626
27* ``testools.TestCase.useFixture`` has been added to glue with fixtures nicely.
28 (Robert Collins)
29
2730
280.9.7310.9.7
29~~~~~32~~~~~
3033
=== modified file 'README'
--- README 2010-10-24 08:46:03 +0000
+++ README 2010-10-26 16:56:50 +0000
@@ -19,14 +19,21 @@
19distributed under the same license as Python, see LICENSE for details.19distributed under the same license as Python, see LICENSE for details.
2020
2121
22Dependencies22Required Dependencies
23------------23---------------------
2424
25 * Python 2.4+ or 3.0+25 * Python 2.4+ or 3.0+
2626
27Optional Dependencies
28---------------------
29
27If you would like to use our undocumented, unsupported Twisted support, then30If you would like to use our undocumented, unsupported Twisted support, then
28you will need Twisted.31you will need Twisted.
2932
33If you want to use ``fixtures`` then you can either install fixtures (e.g. from
34https://launchpad.net/python-fixtures or http://pypi.python.org/pypi/fixtures)
35or alternatively just make sure your fixture objects obey the same protocol.
36
3037
31Bug reports and patches38Bug reports and patches
32-----------------------39-----------------------
3340
=== modified file 'testtools/testcase.py'
--- testtools/testcase.py 2010-10-18 12:46:20 +0000
+++ testtools/testcase.py 2010-10-26 16:56:50 +0000
@@ -538,6 +538,35 @@
538 """538 """
539 return self._get_test_method()()539 return self._get_test_method()()
540540
541 def useFixture(self, fixture):
542 """Use fixture in a test case.
543
544 The fixture will be setUp, and self.addCleanup(fixture.cleanUp) called.
545
546 :param fixture: The fixture to use.
547 :return: The fixture, after setting it up and scheduling a cleanup for
548 it.
549 """
550 fixture.setUp()
551 self.addCleanup(fixture.cleanUp)
552 self.addCleanup(self._gather_details, fixture.getDetails)
553 return fixture
554
555 def _gather_details(self, getDetails):
556 """Merge the details from getDetails() into self.getDetails()."""
557 details = getDetails()
558 my_details = self.getDetails()
559 for name, content_object in details.items():
560 new_name = name
561 disambiguator = itertools.count(1)
562 while new_name in my_details:
563 new_name = '%s-%d' % (name, advance_iterator(disambiguator))
564 name = new_name
565 content_bytes = list(content_object.iter_bytes())
566 content_callback = lambda:content_bytes
567 self.addDetail(name,
568 content.Content(content_object.content_type, content_callback))
569
541 def setUp(self):570 def setUp(self):
542 unittest.TestCase.setUp(self)571 unittest.TestCase.setUp(self)
543 self.__setup_called = True572 self.__setup_called = True
544573
=== modified file 'testtools/tests/test_testtools.py'
--- testtools/tests/test_testtools.py 2010-09-26 20:45:28 +0000
+++ testtools/tests/test_testtools.py 2010-10-26 16:56:50 +0000
@@ -6,6 +6,9 @@
6import sys6import sys
7import unittest7import unittest
88
9import fixtures
10from fixtures.tests.helpers import LoggingFixture
11
9from testtools import (12from testtools import (
10 ErrorHolder,13 ErrorHolder,
11 MultipleExceptions,14 MultipleExceptions,
@@ -13,6 +16,7 @@
13 TestCase,16 TestCase,
14 clone_test_with_new_id,17 clone_test_with_new_id,
15 content,18 content,
19 content_type,
16 skip,20 skip,
17 skipIf,21 skipIf,
18 skipUnless,22 skipUnless,
@@ -1122,6 +1126,59 @@
1122 self.assertIs(marker, value)1126 self.assertIs(marker, value)
11231127
11241128
1129class TestFixtureSupport(TestCase):
1130
1131 def test_useFixture(self):
1132 fixture = LoggingFixture()
1133 class SimpleTest(TestCase):
1134 def test_foo(self):
1135 self.useFixture(fixture)
1136 result = unittest.TestResult()
1137 SimpleTest('test_foo').run(result)
1138 self.assertTrue(result.wasSuccessful())
1139 self.assertEqual(['setUp', 'cleanUp'], fixture.calls)
1140
1141 def test_useFixture_cleanups_raise_caught(self):
1142 calls = []
1143 def raiser(ignored):
1144 calls.append('called')
1145 raise Exception('foo')
1146 fixture = fixtures.FunctionFixture(lambda:None, raiser)
1147 class SimpleTest(TestCase):
1148 def test_foo(self):
1149 self.useFixture(fixture)
1150 result = unittest.TestResult()
1151 SimpleTest('test_foo').run(result)
1152 self.assertFalse(result.wasSuccessful())
1153 self.assertEqual(['called'], calls)
1154
1155 def test_useFixture_details_captured(self):
1156 class DetailsFixture(fixtures.Fixture):
1157 def setUp(self):
1158 fixtures.Fixture.setUp(self)
1159 self.addCleanup(delattr, self, 'content')
1160 self.content = ['content available until cleanUp']
1161 self.addDetail('content',
1162 content.Content(content_type.UTF8_TEXT, self.get_content))
1163 def get_content(self):
1164 return self.content
1165 fixture = DetailsFixture()
1166 class SimpleTest(TestCase):
1167 def test_foo(self):
1168 self.useFixture(fixture)
1169 # Add a colliding detail (both should show up)
1170 self.addDetail('content',
1171 content.Content(content_type.UTF8_TEXT, lambda:['foo']))
1172 result = ExtendedTestResult()
1173 SimpleTest('test_foo').run(result)
1174 self.assertEqual('addSuccess', result._events[-2][0])
1175 details = result._events[-2][2]
1176 self.assertEqual(['content', 'content-1'], sorted(details.keys()))
1177 self.assertEqual('foo', ''.join(details['content'].iter_text()))
1178 self.assertEqual('content available until cleanUp',
1179 ''.join(details['content-1'].iter_text()))
1180
1181
1125def test_suite():1182def test_suite():
1126 from unittest import TestLoader1183 from unittest import TestLoader
1127 return TestLoader().loadTestsFromName(__name__)1184 return TestLoader().loadTestsFromName(__name__)

Subscribers

People subscribed via source and target branches