문제

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

도움이 되었습니까?

해결책

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())
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top