samedi 3 octobre 2015

how do I use unittest.TestResult?

I've only been using unittest for a short time.
(NB I'm using Jython 2.7.10 "final release")

In the Python 2.7 doc 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' and 'blap' (start and end of run) not printed?

On a more general note:
1) 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.
2) can someone possibly tell me why unittest2 often seems to be used instead of unittest?

Aucun commentaire:

Enregistrer un commentaire