How to use unittest.TestResult?

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... . # ... superclass run ended 
+5
source share
3 answers

Wow, no answers. I am surprised.

This is a hack that most people can no doubt decide for themselves if you want something to happen at the beginning of the run and the end of the run:

Subclass TestCase, according to:

 def setUp( self ): if not hasattr( unittest.TestCase, 'app' ): unittest.TestCase.app = MyApp() def shutdown_func(): pass # do any end-of-run stuff here atexit.register( shutdown_func ) pass # do any start-of-run stuff here self.app = unittest.TestCase.app 

Then make all your TestCases subclasses of this ...

The bottom line is that if you want this to happen, your application was built only once. Of course, the responsibility for ensuring that it is "pristine" for each subsequent setUp is up to you. Obviously, you could use setUpClass instead, but then you do not have access to the TestCase instance.

+1
source

unittest2 is a reserve of new unittest features for Python 2.4-2.6. means that if you have a Python 2.4-2.6 script and you want to run 2.7 and later, you should use.

you can find here: https://docs.python.org/2/library/unittest.html

0
source

I have the same problem, so I took a look at the source code.

By checking unittest.TextTestRunner and unittest.TestCase , it looks like startTestRun() and stopTestRun() are getting a manual call. In unittest.TextTestRunner it works like this:

 def run(self, test): # ... startTestRun = getattr(result, 'startTestRun', None) if startTestRun is not None: startTestRun() # ... 

and in your case unittest.TestCase , it is like this:

 def run(self, result=None): orig_result = result if result is None: result = self.defaultTestResult() startTestRun = getattr(result, 'startTestRun', None) if startTestRun is not None: startTestRun() # ... 

So it looks like startTestRun only actually gets a call to TestCase.run() if you don't pass in result . You pass in result , so this does not happen.

This seems like a mistake! But basically this means that you can extend TestCase or TestSuite , override the run method, and then call these methods manually; or just call them outside the appropriate run methods.

Hope this helps!

0
source

Source: https://habr.com/ru/post/1232851/


All Articles