I've only been using unittest for a short time. I am using Jython 2.7.10 "final release"
In the Python 2.7 docs explaining TestResult it says:
The following methods of the TestResult class are used to maintain the internal data structures, and may be extended in subclasses to support additional reporting requirements. This is particularly useful in building tools which support interactive reporting while tests are being run.
startTest(test) ... stopTest(test) ... startTestRun() ... stopTestRun()¶
That's what I want to do... but I can't work out how you use TestResult. Here's an 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 does it say
0 tests
? - Why are
blep
andblap
(start and end of run) not printed?
On a more general note:
Can someone possibly point to a good tutorial/book explaining "proper use"/"good practice" when it comes to TestResult, TestRunner, TestLoader, etc. I got "TDD with Python", but it doesn't seem to explain any of this.
Can someone possibly tell me why unittest2 often seems to be used instead of unittest?
addendum
Following Omar Diab's efforts at looking at the source code I 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 then gave:
# calling superclass run... startTestRun? <bound method TestResult.startTestRun of <unittest.result.TestResult run=0 errors=0 failures=0>>
setUp for test_that_stuff_happened (__main__.xx_FT)
tearDown for test_that_stuff_happened (__main__.xx_FT)
end tearDown...
. # ... superclass run ended