[TIP] Unittest Changes

Jonathan Lange jml at mumak.net
Mon Jul 21 17:10:30 PDT 2008


On Tue, Jul 22, 2008 at 12:49 AM, Daniels, Scott <scott.daniels at hp.com> wrote:
> On Mon, 21 Jul 2008 at 09:54, "Jonathan Lange" <jml at mumak.net> wrote:
>> On Mon, Jul 21, 2008 at 7:16 AM, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
>> > ...
>> > Any other good ideas about ways that unittest could change that don't
>> > either grow the API greatly or break backwards compatibility?
>>
>> The big one is addCleanup (again, implemented & used in pyunit3k, Twisted and bzrlib).
>> The idea is that it allows you to dynamically create resources in tests without worry.
>> Here's an example of usage:
>>
>>     def makeTemporaryDirectory(self):
>>         d = tempfile.mkdtemp()
>>         self.addCleanup(shutil.rmtree, d)
>>         return d
>>
>> Here's a rough implementation:
>>
>> def addCleanup(self, f, *a, **kw):
>>     self._cleanup_stack.append((f, *a, **kw))
>>
>> def _runCleanups(self, test_result):
>>     for f, *a, **kw in self._cleanup_stack:
>>         try:
>>             f(*a, **kw)
>>         except:
>>             test_result.addError(self, sys.exc_info())
>
> I presume you mean:
>> def addCleanup(self, f, *a, **kw):
>>     self._cleanup_stack.append((f, a, kw))
>

I did. When I say "rough", I mean it ;-)

> And I'd suggest popping the stack back, rather than running forwards

Quite right!

We do that in Twisted, Bazaar and Launchpad. I just forgot in my rush
to get the email out.

jml



More information about the testing-in-python mailing list