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

时间:2014-03-03 17:50:13

标签: python unit-testing 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'

输出是:

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

>> OK

我想知道为什么不打印“完成”(或之后发生的任何事情)?

1 个答案:

答案 0 :(得分:12)

exit=False传递给unittest.main()来电(documentation):

unittest.main(exit=False)

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

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

OK
Done

仅供参考,如果TestProgram.runTests()的值为sys.exit()(默认为默认值),则在引擎罩单元测试exit调用True下:

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