软件测试中最基本的组成单元是测试用例(test case), 可以用unittest.TestCase类来创建测试用例。
TestCase
测试用例每一个测试方法:执行前都要用setUp()进行环境初始化,执行完后,用setUp()清除环境。
判断测试结果
TestCase实例中内建了很多判断测试结果的方法,它们都是以assert开头。
常用的判断测试结果方法:
常用判断方法
说明
assertNotEqual(x,y)
x != y
assertEqual(x,y)
x == y
assertTrue(x)
x is True
assertFalse(x)
x is False
assertIs(x,y)
x is y
assertIsNot(x,y)
x not y
assertNot(x)
x is None
assertNotNone(x)
x is not none
assertIn(obj, seq)
obj in seq(对象obj在序列seq中)
assertNotIn(obj, seq)
obj not in seq
assertIsInstance(obj, cls)
isinstance(obj, cls):判断obj是否是cls的实例
assertNotIsInstance(obj, cls)
not isinstance(obj, cls)
示例
先编写个类MyModel:
class MyModel():
def add(self, a, b):
return a + b
def