解決unittest中斷言失敗,後續的斷言都不執行的問題
阿新 • • 發佈:2021-01-10
在unittest測試用例中,在含有多個斷言時,一個斷言失敗後,測試用例會結束執行,無法知道其他斷言是否成功。unittest不同與pytest,在pytest中,
可以使用pytest.assume(表示式),檢查點失敗繼續執行,所以為了解決unittest的這個弊端,本文解決方式: 1)把多個斷言合成一個斷言,判斷多個斷言的綜合結果是True還是False 2)判斷每個斷言結果,失敗則彙總,在最終斷言中輸出 之前多斷言程式碼:1 def checkCMSTab(self, tabLabelInfo, tabName): 2 '''驗證CMS源tab下有無空資料3 驗證tab下有無售罄、下架商品 4 驗證tab下圖片坑位是否配置跳轉連結''' 5 unsaleWareInfos = '' 6 offlineWareInfos = '' 7 unsaleWareInfoList = tabLabelInfo[1] 8 offlineWareInfoList = tabLabelInfo[2] 9 actsInfoList = tabLabelInfo[1] 10 pictureEroorInfos = '' 11 pictureEroorList = []12 13 if len(unsaleWareInfoList) > 0: 14 for ware in unsaleWareInfoList: 15 unsaleWareInfos += '\n導航2Ntab下{tab}-tab{sku}—{wareName}商品已售罄,該商品資料異常,應該被過濾'.format( 16 tab=ware[4],17 sku=ware[0], 18 wareName=ware[1]) 19 # 驗證tab下商品售罄、不可售 20 if len(offlineWareInfoList) > 0: 21 for ware in offlineWareInfoList: 22 offlineWareInfos += '\n導航2Ntab下{tab}-tab{sku}—{wareName}商品已售罄,該商品資料異常,應該被過濾'.format( 23 tab=ware[4], 24 sku=ware[0], 25 wareName=ware[1]) 26 for picture in actsInfoList: 27 if picture[1] == '' or picture[1] is None: 28 pictureEroorList.append(picture) 29 pictureEroorInfos += '\n{}坑位,無跳轉連結' 30 31 32 # 驗證tab下資料空 33 if len(tabLabelInfo[0][0]) + len(tabLabelInfo[1]) == 0: 34 self.assertNotEqual(len(tabLabelInfo[0][0]) + len(tabLabelInfo[1]), 0, 35 '導航2N樓層的{}tab下資料異常:tab資料為空'.format(tabName)) 36 if len(tabLabelInfo[0][0]) == 0: 37 self.assertNotEqual(len(tabLabelInfo[0][0]), 0, 38 '導航2N樓層的{}tab下資料異常:無商品資料'.format(tabName)) 39 40 self.assertLessEqual(len(unsaleWareInfoList), 0, '{tabName}tab下存在已售罄商品,具體資訊為{unsaleWareInfos}'.format( 41 tabName=tabName, 42 unsaleWareInfos=unsaleWareInfos)) 43 self.assertLessEqual(len(offlineWareInfoList), 0, '{tabName}tab下存在已下架商品,具體資訊為{offlineWareInfoList}'.format( 44 tabName=tabName, 45 offlineWareInfoList=offlineWareInfoList)) 46 # 驗證tab下圖片坑位是否無跳轉連結 47 self.assertEqual(len(pictureEroorList), 0, "{tabName}下圖片坑位無跳轉連結,具體資訊為{pictureEroorInfos}".format(tabName=tabName, 48 pictureEroorInfos=pictureEroorInfos))
優化後代碼如下:
def checkCMSTab(self, tabLabelInfo, tabName): '''驗證CMS源tab下有無空資料 驗證tab下有無售罄、下架商品 驗證tab下圖片坑位是否配置跳轉連結''' unsaleWareInfos = '' offlineWareInfos = '' unsaleWareInfoList = tabLabelInfo[1] offlineWareInfoList = tabLabelInfo[2] actsInfoList = tabLabelInfo[1] pictureEroorInfos = '' pictureEroorList = [] if len(unsaleWareInfoList) > 0: for ware in unsaleWareInfoList: unsaleWareInfos += '\n導航2Ntab下{tab}-tab{sku}—{wareName}商品已售罄,該商品資料異常,應該被過濾'.format( tab=ware[4], sku=ware[0], wareName=ware[1]) # 驗證tab下商品售罄、不可售 if len(offlineWareInfoList) > 0: for ware in offlineWareInfoList: offlineWareInfos += '\n導航2Ntab下{tab}-tab{sku}—{wareName}商品已售罄,該商品資料異常,應該被過濾'.format( tab=ware[4], sku=ware[0], wareName=ware[1]) for picture in actsInfoList: if picture[1] == '' or picture[1] is None: pictureEroorList.append(picture) pictureEroorInfos += '\n{}坑位,無跳轉連結' # 解決unittest中斷言失敗,後續的斷言都不執行的問題 res = True msg = [] res = res and (len(tabLabelInfo[0][0]) + len(tabLabelInfo[1]) > 0) # 驗證tab下商品+圖片資料為空 res = res and (len(tabLabelInfo[0][0]) > 0) # 驗證tab下商品為空 res = res and (len(unsaleWareInfoList) == 0) # tab下是否存在已售罄 res = res and (len(offlineWareInfoList) == 0) # tab下是否存在已下架 res = res and (len(pictureEroorList) == 0) # 驗證tab下圖片坑位是否無跳轉連結 if len(tabLabelInfo[0][0]) + len(tabLabelInfo[1]) == 0: msg.append('導航2N樓層的{}tab下資料異常:tab資料為空'.format(tabName)) if len(tabLabelInfo[0][0]) == 0: msg.append('導航2N樓層的{}tab下資料異常:無商品資料'.format(tabName)) if len(unsaleWareInfoList) == 0: msg.append('{tabName}tab下存在已售罄商品,具體資訊為{unsaleWareInfos}'.format(tabName=tabName, unsaleWareInfos=unsaleWareInfos)) if len(offlineWareInfoList) == 0: msg.append('{tabName}tab下存在已下架商品,具體資訊為{offlineWareInfoList}'.format(tabName=tabName, offlineWareInfoList=offlineWareInfoList)) if len(pictureEroorList) == 0: msg.append('{tabName}tab下存在已下架商品,具體資訊為{offlineWareInfoList}'.format(tabName=tabName, offlineWareInfoList=offlineWareInfoList)) self.assertEqual(res, "\n".join(msg))