博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
unittest使用过程中sys.exit(not self.result.wasSuccessful())
阅读量:5069 次
发布时间:2019-06-12

本文共 4710 字,大约阅读时间需要 15 分钟。

起因:

在运行下面的unittest过程中出现了个Traceback:

被测试脚本:

# splitter.pydef 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 fieldsif __name__ == '__main__':    # test myself    import doctest    doctest.testmod()

 测试脚本:

# testsplitter.pyimport splitterimport unittest# unit testclass 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 testif __name__ == '__main__':    unittest.main()

 结果:

...----------------------------------------------------------------------Ran 3 tests in 0.011sOKTraceback (most recent call last):  File "C:\Users\Administrator\Desktop\Python Scripts\testsplitter - 副本.py", line 23, in 
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.011sOKTraceback (most recent call last): File "C:\Users\Administrator\Desktop\Python Scripts\testsplitter - 副本.py", line 23, in
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.

分析:

查找到的链接:

这是一个IDLE问题

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,会是什么样?

修改测试脚本:

# testsplitter.pyimport splitterimport unittest# unit testclass 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 testif __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.077sFAILED (errors=1)Traceback (most recent call last):  File "C:\Users\Administrator\Desktop\Python Scripts\testsplitter - 副本.py", line 23, in 
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里面表示测试用例均通过。

 

转载于:https://www.cnblogs.com/herbert/p/3244614.html

你可能感兴趣的文章
URL和URI的不同
查看>>
接口的使用
查看>>
django中的转义
查看>>
IOS OpenDev下载与安装
查看>>
关于SASS--->推荐使用
查看>>
解析网络 xml 流程
查看>>
第十二周学习报告
查看>>
ConcurrentHashMap源码阅读
查看>>
AspNet MVC中各种上下文理解
查看>>
素数筛 codevs 1675 大质数 2
查看>>
Python小程序—修改haproxy配置文件
查看>>
mybatis foreach 循环 list(map)
查看>>
日期选择器-----tablesorter
查看>>
Android-MediaProvider数据库模式
查看>>
淘宝的公共样式
查看>>
FLASH 和EEPROM区别
查看>>
第七章
查看>>
C++输入密码不显示明文
查看>>
Exp2:后门原理与实践
查看>>
《我最想要的记忆魔法书》笔记
查看>>