Prev: Python-list Digest, Vol 77, Issue 7 *
Next: CheddarGetter module for Python - easy recurring billing
From: Steve Holden on 1 Feb 2010 16:14 Oltmans wrote: > Thank you for your help, Chris. Looks like I can now attach methods to > class 'tee'. However, after attaching methods to 'tee' when I try to > run them using suite.run() I don't see any of the methods running, I'm > sorry but I've no clue what's failing this. Any insights will be > highly appreciated. Here is the sample code > filename: check.py > --- > import inspect > import unittest > > > class result(unittest.TestResult): > > def addSuccess(self,test): > print str(test) + ' succeeded' > def addError(self,test,err): > print 'An error occured while running the test ' + str(test) + > ' and error is = ' + str(err) > def addFailure(self,test,err): > print str(test) + " failed with an error =" + str(err) > > > > class test(unittest.TestCase): > def test_first(self): > print 'first test' > def test_second(self): > print 'second test' > def test_third(self): > print 'third test' > > import new > class tee(unittest.TestCase): > pass > > if __name__=="__main__": > r = result() > for name,func in inspect.getmembers(test,inspect.ismethod): > if name.find('test_')!= -1: > > setattr(tee, name, new.instancemethod(func,None,tee)) > > suite = unittest.defaultTestLoader.loadTestsFromName('check.tee') > suite.run(r) > --- > > Then line suite.run(r) should have run the methods that we just > attached, but it's not. I must be missing something here. Please > enlighten me. > Should not tee be subclassing test, not unittest.TestCase? regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS: http://holdenweb.eventbrite.com/
From: Michele Simionato on 2 Feb 2010 00:47 Wanting the same methods to be attached to different classes often is a code smell (perhaps it is not your case and then use setattr as others said). Perhaps you can just leave such methods outside any class. I would suggest you to use a testing framework not based on inheritance (i.e. use nose or py.test). Perhaps your problem can be solved with generative tests.
From: Oltmans on 2 Feb 2010 00:55 On Feb 2, 2:14 am, Steve Holden <st...(a)holdenweb.com> wrote: > > Should not tee be subclassing test, not unittest.TestCase? > Thank you, Steve. This worked, but I've not clue why? Can you please enlighten me why sub-classing 'test' made it happen? Please. Thanks again. > regards > Steve > -- > Steve Holden +1 571 484 6266 +1 800 494 3119 > PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ > Holden Web LLC http://www.holdenweb.com/ > UPCOMING EVENTS: http://holdenweb.eventbrite.com/- Hide quoted text - > > - Show quoted text -
From: Steve Holden on 2 Feb 2010 01:06 Oltmans wrote: > On Feb 2, 2:14 am, Steve Holden <st...(a)holdenweb.com> wrote: >> Should not tee be subclassing test, not unittest.TestCase? >> > > Thank you, Steve. This worked, but I've not clue why? Can you please > enlighten me why sub-classing 'test' made it happen? Please. Thanks > again. unittest.TestCase doesn't have any test* methods (methods whose names begin with "test"). So neither do your subclasses. When you subclass test, however, test has everything that unittest.TestCase does (because it subclasses unittest.TestCase) and it also has three test* methods. So your subclass of test also has those three methods as well. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS: http://holdenweb.eventbrite.com/
First
|
Prev
|
Pages: 1 2 Prev: Python-list Digest, Vol 77, Issue 7 * Next: CheddarGetter module for Python - easy recurring billing |