我是靠谱客的博主 愉快画笔,最近开发中收集的这篇文章主要介绍Unittest单元测试系列[v1.0.0][subTest],觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

代码实例

# coding:utf-8
import unittest
class NumbersTest(unittest.TestCase):
def test_even(self):
"""
使用subTest上下文管理器,区分细小的变化
取模运算,返回除法的余数,但是参数是0到5的整数,没必要单独写方法
"""
for i in range(0, 6):
with self.subTest(i=i):
self.assertEqual(i % 2, 0)
if __name__ == '__main__':
unittest.main()

执行这段代码的结果会是:

SubTest failure: Traceback (most recent call last):
File "C:Python37libunittestcase.py", line 59, in testPartExecutor
yield
File "C:Python37libunittestcase.py", line 533, in subTest
yield
File "D:ProgramsPythonDemounittest4subtestDemo.py", line 14, in test_even
self.assertEqual(i % 2, 0)
File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharmteamcitydiff_tools.py", line 39, in _patched_equals
raise native_error
File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharmteamcitydiff_tools.py", line 32, in _patched_equals
old(self, first, second, msg)
File "C:Python37libunittestcase.py", line 839, in assertEqual
assertion_func(first, second, msg=msg)
File "C:Python37libunittestcase.py", line 832, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 1 != 0
SubTest failure: Traceback (most recent call last):
File "C:Python37libunittestcase.py", line 59, in testPartExecutor
yield
File "C:Python37libunittestcase.py", line 533, in subTest
yield
File "D:ProgramsPythonDemounittest4subtestDemo.py", line 14, in test_even
self.assertEqual(i % 2, 0)
File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharmteamcitydiff_tools.py", line 39, in _patched_equals
raise native_error
File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharmteamcitydiff_tools.py", line 32, in _patched_equals
old(self, first, second, msg)
File "C:Python37libunittestcase.py", line 839, in assertEqual
assertion_func(first, second, msg=msg)
File "C:Python37libunittestcase.py", line 832, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 1 != 0
SubTest failure: Traceback (most recent call last):
File "C:Python37libunittestcase.py", line 59, in testPartExecutor
yield
File "C:Python37libunittestcase.py", line 533, in subTest
yield
File "D:ProgramsPythonDemounittest4subtestDemo.py", line 14, in test_even
self.assertEqual(i % 2, 0)
File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharmteamcitydiff_tools.py", line 39, in _patched_equals
raise native_error
File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharmteamcitydiff_tools.py", line 32, in _patched_equals
old(self, first, second, msg)
File "C:Python37libunittestcase.py", line 839, in assertEqual
assertion_func(first, second, msg=msg)
File "C:Python37libunittestcase.py", line 832, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 1 != 0
One or more subtests failed
Failed subtests list: (i=1), (i=3), (i=5)
Ran 1 test in 0.020s
FAILED (failures=3)
Process finished with exit code 1

而如果我们不使用subTest(), 只是写个简单的循环去断言,当程序执行到第一个断言失败时就会终止了,后边可能还有断言能够成功的也就不会被执行了

# coding:utf-8
import unittest
class NumbersTest(unittest.TestCase):
def test_even(self):
for i in range(0, 6):
# with self.subTest(i=i):
print("当前参数是:%d" % i)
self.assertEqual(i % 2, 0)
if __name__ == '__main__':
unittest.main()

执行结果会是:

当前参数是:0
当前参数是:1
Ran 1 test in 0.010s
FAILED (failures=1)
0 != 1
Expected :1
Actual
:0
<Click to see difference>
Traceback (most recent call last):
File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharmteamcitydiff_tools.py", line 32, in _patched_equals
old(self, first, second, msg)
File "C:Python37libunittestcase.py", line 839, in assertEqual
assertion_func(first, second, msg=msg)
File "C:Python37libunittestcase.py", line 832, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 1 != 0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:Python37libunittestcase.py", line 59, in testPartExecutor
yield
File "C:Python37libunittestcase.py", line 615, in run
testMethod()
File "D:ProgramsPythonDemounittest4subtestDemo.py", line 15, in test_even
self.assertEqual(i % 2, 0)
Process finished with exit code 1

最后

以上就是愉快画笔为你收集整理的Unittest单元测试系列[v1.0.0][subTest]的全部内容,希望文章能够帮你解决Unittest单元测试系列[v1.0.0][subTest]所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(28)

评论列表共有 0 条评论

立即
投稿
返回
顶部