unittest使用過程當中sys.exit(not self.result.wasSuccessful())

原由:

在運行下面的unittest過程當中出現了個Traceback:python

被測試腳本:函數

# splitter.py
def split(line, types=None, delimiter=None):
    """Splits a line of test and optionally performs type conversion.
    For example:

    >>> split('GOOD 100 490.50')
    ['GOOD', '100', '490.50']
    >>> split('GOOD 100 490.50', [str, int, float])
    ['GOOD', 100, 490.50]
    >>>
    By default, splitting is perfomed on whitespace, but a different delimiter
    can be selected with the delimiter keyword argument:

    >>> split('GOOD, 100, 490.50', delimiter=',')
    ['GOOOD', '100', '490.50']
    >>>
    """

    fields = line.split(delimiter)
    if types:
        fields = [ty(val) for ty, val in zip(types, fields)]
    return fields

if __name__ == '__main__':
    # test myself
    import doctest
    doctest.testmod()

 測試腳本:測試

# testsplitter.py
import splitter
import unittest

# unit test
class TestSplitFunction(unittest.TestCase):
    def setUp(self):
        pass
    def tearDown(self):
        pass
    def testsimplestring(self):
        r = splitter.split('GOOD 100 490.50')
        self.assertEqual(r, ['GOOD', '100', '490.50'])
    def testypeconvert(self):
        r = splitter.split('GOOD 100 490.50', [str, int, float])
        self.assertAlmostEqual(r, ['GOOD', 100, 490.50])
    def testdelimiter(self):
        r = splitter.split('GOOD,100,490.50', delimiter=',')
        self.assertEqual(r, ['GOOD', '100', '490.50'])

# Run unit test
if __name__ == '__main__':
    unittest.main()

 結果:this

...
----------------------------------------------------------------------
Ran 3 tests in 0.011s

OK
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\Python Scripts\testsplitter - 副本.py", line 23, in <module>
    unittest.main()
  File "D:\Python33\lib\unittest\main.py", line 125, in __init__
    self.runTests()
  File "D:\Python33\lib\unittest\main.py", line 267, in runTests
    sys.exit(not self.result.wasSuccessful())
SystemExit: False
>>> ================================ RESTART ================================
>>> 
...
----------------------------------------------------------------------
Ran 3 tests in 0.011s

OK
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\Python Scripts\testsplitter - 副本.py", line 23, in <module>
    unittest.main()
  File "D:\Python33\lib\unittest\main.py", line 125, in __init__
    self.runTests()
  File "D:\Python33\lib\unittest\main.py", line 267, in runTests
    sys.exit(not self.result.wasSuccessful())
SystemExit: False

 從結果看,全部測試用例是都經過的,可是出現了Traceback,SystemExit: False.spa

分析:

查找到的連接: http://bugs.python.org/issue2821orm

這是一個IDLE問題blog

IDLE catches the SystemExit function raised by TestProgram().runTests()
and prints the traceback. Not a bug in unittest.

由於unittest.main()函數會調用sys.exit()來結束函數進程。使用的參數就是not self.result.wasSuccessful()進程

進一步分析:

那若是是assert過程當中出現fail,會是什麼樣?ip

修改測試腳本:get

# testsplitter.py
import splitter
import unittest

# unit test
class TestSplitFunction(unittest.TestCase):
    def setUp(self):
        pass
    def tearDown(self):
        pass
    def testsimplestring(self):
        r = splitter.split('GOOD 100 490.50')
        self.assertEqual(r, ['GOOD', '100', '490.50'])
    def testypeconvert(self):
        r = splitter.split('GOOD 100 490.50', [str, int, float])
        self.assertAlmostEqual(r, ['GOOD', 10, 490.50])  # Modify this line
    def testdelimiter(self):
        r = splitter.split('GOOD,100,490.50', delimiter=',')
        self.assertEqual(r, ['GOOD', '100', '490.50'])

# Run unit test
if __name__ == '__main__':
    unittest.main()

 運行結果:

..E
======================================================================
ERROR: testypeconvert (__main__.TestSplitFunction)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\Python Scripts\testsplitter - 副本.py", line 16, in testypeconvert
    self.assertAlmostEqual(r, ['GOOD', 10, 490.50])
  File "D:\Python33\lib\unittest\case.py", line 683, in assertAlmostEqual
    if round(abs(second-first), places) == 0:
TypeError: unsupported operand type(s) for -: 'list' and 'list'

----------------------------------------------------------------------
Ran 3 tests in 0.077s

FAILED (errors=1)
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\Python Scripts\testsplitter - 副本.py", line 23, in <module>
    unittest.main()
  File "D:\Python33\lib\unittest\main.py", line 125, in __init__
    self.runTests()
  File "D:\Python33\lib\unittest\main.py", line 267, in runTests
    sys.exit(not self.result.wasSuccessful())
SystemExit: True

 

分析:

unittest裏面的assert和通常的assert是不同的,即便fail了,其實仍是會繼續執行其餘的測試用例,不會當即跳出函數。最後跳出函數仍是調用sys.exit(),只不過這個時候的參數就是True了。

相似sys.exit(1) - 表示非正常退出。在unittest裏面表示有測試用例失敗。

sys.exist(0) - 正常退出。在unittest裏面表示測試用例均經過。

相關文章
相關標籤/搜索