python - How do I use unittest.TestResult? -
i've been using unittest short time. using jython 2.7.10 "final release"
in python 2.7 docs explaining testresult says:
the following methods of testresult class used maintain internal data structures, , may extended in subclasses support additional reporting requirements. particularly useful in building tools support interactive reporting while tests being run.
starttest(test) ... stoptest(test) ... starttestrun() ... stoptestrun()¶
that's want do... can't work out how use testresult. here's sscce...
import unittest class testresultx( unittest.testresult ): def starttest( self, test ): print( '# blip') unittest.testresult.starttest( self, test ) def stoptest( self, test ): print( '# blop') unittest.testresult.stoptest( self, test ) def starttestrun( self ): print( '# blep') unittest.testresult.starttestrun( self ) def stoptestrun( self ): print( '# blap') unittest.testresult.stoptestrun( self ) class testcasex( unittest.testcase ): def test_nonsense(self): print( '# wotcha' ) self.asserttrue( false ) def run( self, test_result=none ): print( '# spoons starting...') test_result = testresultx() unittest.testcase.run( self, test_result ) print( '# ...spoons ended, tr %s' % ( test_result, ) ) unittest.main()
results in:
# spoons starting... ---------------------------------------------------------------------- ran 0 tests in 0.015s ok # blip # wotcha # blop # ...spoons ended, tr <__main__.testresultx run=1 errors=0 failures=1>
questions:
- why
0 tests
? - why
blep
,blap
(start , end of run) not printed?
on more general note:
can possibly point tutorial/book explaining "proper use"/"good practice" when comes testresult, testrunner, testloader, etc. got "tdd python", doesn't seem explain of this.
can possibly tell me why unittest2 seems used instead of unittest?
addendum
following omar diab's efforts @ looking @ source code tried this:
def run( self, *args, **kvargs ): result = self.defaulttestresult() starttestrun = getattr(result, 'starttestrun', none) logger.info( '# calling superclass run... starttestrun? %s' % ( starttestrun, )) unittest.testcase.run( self, *args, **kvargs ) logger.info( '# ... superclass run ended')
unfortunately each test_xxx method gave:
# calling superclass run... starttestrun? <bound method testresult.starttestrun of <unittest.result.testresult run=0 errors=0 failures=0>> setup test_that_stuff_happened (__main__.xx_ft) teardown test_that_stuff_happened (__main__.xx_ft) end teardown... . # ... superclass run ended
unittest2 backport of new unittest features python 2.4-2.6 means that, if have python 2.4-2.6 script , want run in 2.7 , later version, should use.
you can find details here: https://docs.python.org/2/library/unittest.html
Comments
Post a Comment