Adding methods from one class to another, dynamically
Steve Holden
steve at holdenweb.com
Mon Feb 1 16:14:51 EST 2010
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/
More information about the Python-list
mailing list