Re: [Pyunit-interest] Feature Request: Cleanup Registry
Brought to you by:
purcell
From: Steve P. <ste...@ya...> - 2002-07-18 07:23:07
|
Hi Jim, Sorry for the slow response. Interesting suggestion. I wonder about a couple of things. Firstly, not every test would require every cleanup function to be called, so calling every function after every test seems a high price to pay for convenience. Imagine running 10,000 tests, where each block of 100 requires a single (but different) clean-up function: that's 1,000,000 cleanup function invocations instead of 10,000. Secondly, I'm nervous about the notion of adding clean-up functions to the clean-up registry from 'outside' testing code, as in the example you provided. What if the test module is imported multiple times, e.g. by the unittest GUI? -- then, clean-up functions might be registered multiple times, presumably in global variables in the unittest module. Cleaning-up of component registries is to be done after every Zope test, as I understand things. In that case, I'd think a common base class for your Zope test cases is in order, with a standardised 'tearDown()' that all subclasses should call if they also implement a 'tearDown()' method. This is a safer solution, in my opinion. Or am I missing something? Very best wishes, -Steve Jim Fulton wrote: > Problem > > the Zope 3 project is making extensive use of PyUnit. Zope 3 unit > tests often have to register components with various component > registries. These registrations need to be torn down after each > test. This became very tedious. For this reason, we've implemented a > cleanup registry. Any stateful global objects, like component > registries register functions with this cleanup registry. Test > classes provide tearDown methods that call a method on the cleanup > registry, which calls all the registered functions. This is an > extremely useful pattern that might be beneficial to other > projects. Further, it would be nice if test classes didn't have to > provide a tear-down method that just called the cleanup registry (or > mix-in a class that provided such a tearDown. > > Proposal > > Add a cleanup registry to the unittest module. The unittest module > would provide an additional function, addCleanUp, which takes a > callable object to be called without arguments after running each > unit test. After each test, and after running the test class's > tearDown method, each registered cleanup callable will be called. > > Suppose there was an application module with a global dictionary > that was manipulated by application routines: > > TheData = {} > > The module would register the clear method of this dictionary with > unittest.addCleanUp to make sure the dictionary was reinitialized > after each test: > > import unittest > unittest.addCleanUp(TheData.clear) > > I'd be happy to provide a patch. > > Jim |