Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't python execute anything after 'unittest.main()' gets executed?

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)?

like image 271
skamsie Avatar asked Mar 03 '14 17:03

skamsie


1 Answers

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())
like image 141
alecxe Avatar answered Oct 04 '22 03:10

alecxe