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{}坑位,无跳转链接'
# 验证tab下数据空
self.assertNotEqual(len(tabLabelInfo[0][0]) + len(tabLabelInfo[1]), 0,
'导航2N楼层的{}tab下数据异常:tab数据为空'.format(tabName))
self.assertNotEqual(len(tabLabelInfo[0][0]), 0,
'导航2N楼层的{}tab下数据异常:无商品数据'.format(tabName))
self.assertEqual(len(unsaleWareInfoList), 0, '{tabName}tab下存在已售罄商品,具体信息为{unsaleWareInfos}'.format(
tabName=tabName,
unsaleWareInfos=unsaleWareInfos))
self.assertEqual(len(offlineWareInfoList), 0, '{tabName}tab下存在已下架商品,具体信息为{offlineWareInfoList}'.format(
tabName=tabName,
offlineWareInfoList=offlineWareInfoList))
# 验证tab下图片坑位是否无跳转链接
self.assertEqual(len(pictureEroorList), 0, "{tabName}下图片坑位无跳转链接,具体信息为{pictureEroorInfos}".format(tabName=tabName,
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}下图片坑位无跳转链接,具体信息为{pictureEroorInfos}".format(tabName=tabName,
pictureEroorInfos=pictureEroorInfos))
self.assertEqual(res, "\n".join(msg))