Re: [Pyunit-interest] Feature Request: Cleanup Registry
Brought to you by:
purcell
From: Ype K. <yk...@xs...> - 2002-07-09 20:18:42
|
Jim, you wrote: >My request and yours are not all that similar. I'd still like a response >to *my* request. > >Jim > you wrote earlier: >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. You're right, it's in the tearDown() of a test and not in the tearDown() of a test suite. In case cleaning the registry is your only tearDown() functionality with a mix-in you end up defining: class RegistryCleaner: def tearDown(self): theRegistry.cleanup() class someZope3TestCase( RegistryCleaner, unittest.testcase): .... In case mixing in is too tedious you can use straight inheritance: class RegistryCleaningTestCase( unittest.testcase): def tearDown(self): theRegistry.cleanup() # No test...() methods here. # Evt. move out of module scope to prevent searching for test...() methods. class someZope3TestCase( RegistryCleaningTestCase): # test... methods here When you need more tearDown() functionality depending on the actual test case class, you can call back: class RegistryCleaner: def tearDown(self): theRegistry.cleanup() self.tearDown2() # if hasattr(self, 'tearDown2') and define tearDown2() instead of tearDown() in someZope3TestCase. Here it would be nice to be able to call all tearDown() methods in a multiple inheritance hierarchy, but I don't know how to do that in python. I don't think any of this is tedious, and I guess even more compact ways are possible. Have fun, Ype -- |