码迷,mamicode.com
首页 > 其他好文 > 详细

解决unittest断言失败,后续的断言不执行进阶之重新封装断言

时间:2021-04-22 15:35:52      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:bsp   rtl   less   version   end   ini   否则   import   fir   

python unintest单元测试框架提供了一整套内置的断言方法。

   1. 如果断言失败,则抛出一个AssertionError,并标识该测试为失败状态。

   2. 如果成功,则标识该测试为成功状态。

但存在一个缺点,测试用例中含有多个断言时,一个断言失败,抛出异常,后续断言不再继续执行,也就无从知道后面断言结果。如果使用Python+pytest框架,pytest中的pytest-assume 插件,一个可以允许pytest测试用例中执行多个失败的断言的插件(即断言1,断言2,断言3都失败的情况下,三个断言都能被执行。针对unittest无对应自带的方法可以优化断言失败的问题。但我们可以重写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
  9 
 10 
 11 class CheckPoint(unittest.TestCase):
 12 
 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 
 56     def checkAssertIs(self, arg1, arg2, msg=None):
 57         """验证arg1、arg2是同一个对象,不是则fail"""
 58         try:
 59             self.assertIs(arg1, arg2, msg)
 60         except Exception as e:
 61             self.flag += 1
 62             self.msg.append("\n{}".format(msg))
 63             print(e)
 64 
 65     def checkAssertIsNot(self, arg1, arg2, msg=None):
 66         """验证arg1、arg2不是同一个对象,是则fail"""
 67         try:
 68             self.assertIsNot(arg1, arg2, msg)
 69         except Exception as e:
 70             self.flag += 1
 71             self.msg.append("\n{}".format(msg))
 72             print(e)
 73 
 74     def checkAssertIsNone(self, expr, msg=None):
 75         """    验证expr是None,不是则fail"""
 76         try:
 77             self.assertIsNone(expr, msg)
 78         except Exception as e:
 79             self.flag += 1
 80             self.msg.append("\n{}".format(msg))
 81             print(e)
 82 
 83     def checkAssertIsNotNone(self, expr, msg=None):
 84         """    验证expr不是None,是则fail"""
 85         try:
 86             self.assertIsNotNone(expr, msg)
 87         except Exception as e:
 88             self.flag += 1
 89             self.msg.append("\n{}".format(msg))
 90             print(e)
 91 
 92     def checkAssertIn(self, arg1, arg2, msg=None):
 93         """    验证arg1是arg2的子串,不是则fail"""
 94         try:
 95             self.assertIn(arg1, arg2, msg)
 96         except Exception as e:
 97             self.flag += 1
 98             self.msg.append("\n{}".format(msg))
 99             print(e)
100 
101     def checkAssertNotIn(self, arg1, arg2, msg=None):
102         """验证arg1不是arg2的子串,是则fail"""
103         try:
104             self.assertNotIn(arg1, arg2, msg)
105         except Exception as e:
106             self.flag += 1
107             self.msg.append("\n{}".format(msg))
108             print(e)
109 
110     def checkAssertIsInstance(self, arg1, arg2, msg=None):
111         """验证obj是cls的实例,不是则fail"""
112         try:
113             self.assertIsInstance(arg1, arg2, msg)
114         except Exception as e:
115             self.flag += 1
116             self.msg.append("\n{}".format(msg))
117             print(e)
118 
119     def checkAssertNotIsInstance(self, arg1, arg2, msg=None):
120         """验证obj不是cls的实例,是则fail"""
121         try:
122             self.assertNotIsInstance(arg1, arg2, msg)
123         except Exception as e:
124             self.flag += 1
125             self.msg.append("\n{}".format(msg))
126             print(e)
127 
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         """获取用例执行结果"""
167         return self.assertEqual(self.flag, 0, "{}".format(self.msg))

 

解决unittest断言失败,后续的断言不执行进阶之重新封装断言

标签:bsp   rtl   less   version   end   ini   否则   import   fir   

原文地址:https://www.cnblogs.com/dy99/p/14684450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!