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把兩次斷言失敗的異常信息都有收集到,所以此方法可行。
碼字不易,如有轉載請注明出處,感謝~
