为什么在执行'unittest.main()'之后python不执行任何操作?

ska*_*sie 9 python testing unit-testing python-unittest

所以,假设我有以下内容:

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'
Run Code Online (Sandbox Code Playgroud)

输出是:

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

>> OK
Run Code Online (Sandbox Code Playgroud)

我想知道为什么不打印"完成"(或任何后来的东西)?

ale*_*cxe 14

传递exit=Falseunittest.main()调用(文档):

unittest.main(exit=False)
Run Code Online (Sandbox Code Playgroud)

这是我在控制台上得到的内容:

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

OK
Done
Run Code Online (Sandbox Code Playgroud)

仅供参考,引擎盖下的单元测试的TestProgram.runTests()电话sys.exit()如果价值exit就是True(这是默认设置):

def runTests(self):
    ...
    if self.exit:
        sys.exit(not self.result.wasSuccessful())
Run Code Online (Sandbox Code Playgroud)