unit testing failure makes no sense

Fernando Perez fperez.net at gmail.com
Wed Sep 6 07:22:52 CEST 2006

listservs at mac.com wrote:

> I have some unit testing code in one of my modules that appears to
> run without an error, but the unit test fails anyhow. Have a look at
> the output below -- the TestResult seems to have no errors and no
> failures, yet I get a system exit.

unittest.main() ALWAYS raises system exit:

unittest.py in runTests(self=<unittest.TestProgram object at 0x10ee670>)
     795             self.testRunner = TextTestRunner 
     796         result = self.testRunner.run(self.test)
--> 797         sys.exit(not result.wasSuccessful())
         global sys.exit = <built-in function exit>
         result.wasSuccessful = <bound method  
_TextTestResult.wasSuccessful of <unittest._TextTestResult run=3  
errors=0 failures=0>>
     799 main = TestProgram

You normally don't see it b/c the python interpeter doesn't flag SystemExits
at the very end (I'm not sure of the details there).

For this very reason, ipython's run magic has a -e flag, which makes it
ignore SystemExit exceptions, and lets you run your unit tests from within
ipython in peace.

Next time, ask this one on the ipython list: it was a pure accident that I
caught it, I have unfortunately no time left for c.l.py these days.



More information about the Python-list mailing list