解决unittest断言失败,后续的断言不执行进阶之重新封装断言
python unintest单元测试框架提供了一整套内置的断言方法。
1. 如果断言失败,则抛出一个AssertionError,并标识该测试为失败状态。
2. 如果成功,则标识该测试为成功状态。
但存在一个缺点,测试用例中含有多个断言时,一个断言失败,抛出异常,后续断言不再继续执行,也就无从知道后面断言结果。如果使用Python+pytest框架,pytest中的pytest-assume 插件,一个可以允许pytest测试用例中执行多个失败的断言的插件(即断言1,断言2,断言3都失败的情况下,三个断言都能被执行。针对unittest无对应自带的方法可以优化断言失败的问题,找了好些文档,没发现比较好的方法,朋友推荐说可以使用subtest方法,资料比较少没找到好的运用方法,以后继续尝试吧,有了会贴上来,现在先来介绍重写unittest断言:
1 """
2 # File : checkPoint.py
3 # Time :2021/4/20 14:05
4 # Author :DY
5 # version :V1.0.0
6 # Description:重写unittest断言方法,解决断言失败不再继续执行的问题
7 """
8 import unittest
11 class CheckPoint(unittest.TestCase):
13 def __init__(self, methodName='runTest'):
14 super(CheckPoint, self).__init__(methodName)
15 self._testMethodName = methodName
16 self.flag = 0
17 self.msg = []
18
19 # 基本的布尔断言:要么正确,要么错误的验证
20 def checkAssertEqual(self, arg1, arg2, msg=None):
21 """ 验证arg1=arg2,不等则fail"""
22 try:
23 self.assertEqual(arg1, arg2, msg)
24 except Exception as e:
25 self.flag += 1
26 self.msg.append("\n{}".format(msg))
27 print(e)
28
29 def checkAssertNotEqual(self, arg1, arg2, msg=None):
30 """ 验证arg1 != arg2, 相等则fail"""
31 try:
32 self.assertNotEqual(arg1, arg2, msg)
33 except Exception as e:
34 self.flag += 1
35 self.msg.append("\n{}".format(msg))
36 print(e)
37
38 def checkAssertTrue(self, expr, msg=None):
39 """验证expr是true,如果为false,则fail"""
40 try:
41 self.assertTrue(expr, msg)
42 except Exception as e:
43 self.flag += 1
44 self.msg.append("\n{}".format(msg))
45 print(e)
46
47 def checkAssertFalse(self, expr, msg=None):
48 """ 验证expr是false,如果为true,则fail"""
49 try:
50 self.assertFalse(expr, msg)
51 except Exception as e:
52 self.flag += 1
53 self.msg.append("\n{}".format(msg))
54 print(e)
55
128 # 比较断言:比较两个变量的值
129 def checkAssertGreater(self, first, second, msg=None):
130 """ 验证first > second,否则fail"""
131 try:
132 self.assertGreater(first, second)
133 except Exception as e:
134 self.flag += 1
135 self.msg.append("\n{}".format(msg))
136 print(e)
137
138 def checkAssertGreaterEqual(self, first, second, msg=None):
139 """验证first >= second,否则fail"""
140 try:
141 self.assertGreaterEqual(first, second)
142 except Exception as e:
143 self.flag += 1
144 self.msg.append("\n{}".format(msg))
145 print(e)
146
147 def checkAssertLess(self, first, second, msg=None):
148 """验证first < second,否则fail"""
149 try:
150 self.assertLess(first, second)
151 except Exception as e:
152 self.flag += 1
153 self.msg.append("\n{}".format(msg))
154 print(e)
155
156 def checkAssertLessEqual(self, first, second, msg=None):
157 """验证first <= second,否则fail"""
158 try:
159 self.assertLessEqual(first, second)
160 except Exception as e:
161 self.flag += 1
162 self.msg.append("\n{}".format(msg))
163 print(e)
164
165 def checkTestResult(self):
166 """获取用例执行结果,断言flag是否为0,不为0说明测试用例中存在断言失败"""
167 return self.assertEqual(self.flag, 0, "{}".format(self.msg))
测试用例的引用:测试用例继承父类checkPoint,使用封装的测试用例
1 from util.checkPoint import CheckPoint
2
3 class HomePage(CheckPoint):
4 def test_001(self):
5 self.checkAssertEqual(1, 1, "断言失败")
6 self.checkAssertEqual(1, 2, "断言失败")
7 self.checkAssertEqual(1, 3, "断言不咋正确")
8 self.checkTestResult()
9 if __name__ == '__main__':
10 unittest.main()
执行结果:
1 != 2 : 断言失败 1 != 3 : 断言不咋正确 ['\n断言失败', '\n断言不咋正确'] 0 != 2 Expected :2 Actual :0 <Click to see difference> Traceback (most recent call last): File "D:\DmallAutoPyCMSDY\question.py", line 235, in test_001 self.checkTestResult() File "D:\DmallAutoPyCMSDY\util\checkPoint.py", line 167, in checkTestResult return self.assertEqual(self.flag, 0, "{}".format(self.msg)) AssertionError: 2 != 0 : ['\n断言失败', '\n断言不咋正确']
用例test_001的第2/3条断言都是失败的,通过执行结果我们可以看出,第2条断言执行失败后,继续执行第3条用例,并且msg把两次断言失败的异常信息都有收集到,所以此方法可行。
码字不易,如有转载请注明出处,感谢~
每天进步一小步 日积月累跨大步