[Pyunit-interest] Idea of 'suite' method in TestCase
Brought to you by:
purcell
From: Steve P. <ste...@ya...> - 2000-07-03 04:02:23
|
Hi again, A few people have suggested the addition of a 'suite' factory method to the TestCase class. Now, at the weekend I also decided that this might be a nice idea. I figured out why it's not so easy to do, though. What is really wanted is a 'static' method in TestCase, to use Java's terminology: class MyTestCase(unittest.TestCase): def suite(): return unittest.makeSuite(MyTestCase) Then, a suite could be obtained using 'MyTestCase.suite()'. Of course, this doesn't work because 'suite' can only be called on an instance of the class, and we don't want to have to create a TestCase just to call its 'suite' method and then dump it. I tend to just create outboard TestSuite subclasses or factory functions named after the associated TestCase, i.e. TrampolineTestSuite for TrampolineTestCase. But how about: class MyTestCase(unittest.TestCase): class suite(unittest.TestSuite): def __init__(self): unittest.TestSuite.__init__(self,(unittest.makeSuite(MyTestCase),)) ... or even: class StaticMethod: def __init__(self, func, *args, **kwargs): self.func = func self.args = args self.kwargs = kwargs def __call__(self): return apply(self.func, self.args, self.kwargs) class MyTestCase(unittest.TestCase): ... MyTestCase.suite = StaticMethod(unittest.makeSuite, MyTestCase) Not particularly nice, I admit, but it you want a suite for each TestCase, one of these ideas might help you. Incidentally, if you use the first code snippet above then subclass your TestCases from 'MyTestCase' rather than 'TestCase', you will then have a 'suite' pseudo-method in each of your subclasses. I don't think I advocate this, so I'm probably just corrupting your sensible minds and causing trouble. Again. :-) Best wishes to all, -Steve -- Steve Purcell, Technical Director, Inkontact Get in touch at http://www.inkontact.com/ "Life must be simple if I can do it" -- Me |