I use unittest for a short time. I am using Jython 2.7.10 "final release"
Python 2.7 docs explaining TestResult says:
The following methods of the TestResult class are used for internal data structures and can be extended into subclasses to support additional reporting requirements. This is especially useful in tools that support online reporting while tests are running.
startTest (test) ... stopTest (test) ... startTestRun () ... stopTestRun () ¶
This is what I want to do ... but I cannot understand how you use TestResult. Here is 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 he say
0 tests ? - Why don't
blep and blap (start and end of run) print?
In a more general note:
Can someone please point out 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 this.
Can someone tell me why unittest2 is often used instead of unittest?
addition
Following the efforts of Omar Diab to study 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 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... .