为什么 python 在执行 'unittest.main()' 后不执行任何内容?



假设我有以下内容:

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'

输出为:

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

我想知道为什么不能打印"完成"(或之后的任何内容)?

exit=False传递给unittest.main()调用(文档):

unittest.main(exit=False)

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

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

仅供参考,在引擎盖下,unittest 的TestProgram.runTests()调用sys.exit()如果 exit 的值True(默认情况下):

def runTests(self):
    ...
    if self.exit:
        sys.exit(not self.result.wasSuccessful())

相关内容

最新更新