Question

So let's say I have the following:

import unittest

class MyTests(unittest.TestCase):

  def test001(self):
    print 'This is test001'

  def test002(self):
    print 'This is test002'

if __name__ == '__main__':
  unittest.main()
  print 'Done'

And the output is:

>> This is test001
>> This is test002
>> ----------------------------------------------------------------------
>> Ran 2 tests in 0.001s

>> OK

And I was wondering why doesn't get to print 'Done' (or anything that comes after)?

Was it helpful?

Solution

Pass exit=False to the unittest.main() call (documentation):

unittest.main(exit=False)

Here's what I'm getting on the console:

$ python test.py
This is test001
.This is test002
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
Done

FYI, under the hood unittest's TestProgram.runTests() calls sys.exit() if the value of exit is True (which is by default):

def runTests(self):
    ...
    if self.exit:
        sys.exit(not self.result.wasSuccessful())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top