一、Django整体测试框架
1、生成Django项目中--sign文件--tests.py文件中为Django的测试case管理文件
Django下面的测试模块(上图中的TestCase)是继承python下面的unit test(单元测试框架)下面的TestCase模块
2、在Python中创建的所有类默认都是继承object
二、unittest
1、创建类的三种方式
class Calculator: class Calculator(): class Calculator(object):
2、简单的单元测试举例
2.1、实现简单的计算器test.py文件
class Calculator(object): def add(self,a,b): return a + b def sub(self,a ,b): return a - b
2.2、测试用例编写
2.2.1、不用unittest框架实现
count_test.py文件
import test c = test.Calculator() #引入test文件中的Calculator类 result = c.add(3,5) #调用Calculator类下面的add方法 print (result)
验证利用assert方法进行验证实际结果与预期结果知否一致
import test c = test.Calculator() result = c.add(3,5) print (result) assert result == 8
2.2.2、用unittest框架实现
- 必须创建一个测试类,不能直接调用,且类必须继承unittest下面的TestCase
- 创建测试方法,测试方法必须以test开头
- 断言方法用unittest自带的,如果要使用父类下面的方法的话需要self.方法
- unittest中用main函数来执行case
- assertEqual方法的参数定义:所有类下面的方法第一个参数都必须是self,first = case执行的结果result,second = 预期的结果,msg = 如果实际与预期不符合的话抛出相对应的异常
def assertEqual(self, first, second, msg=None): """Fail if the two objects are unequal as determined by the ‘==‘ operator. """ assertion_func = self._getAssertEqualityFunc(first, second) assertion_func(first, second, msg=msg)
2.2.3、case
import test import unittest class MyTest(unittest.TestCase): #必须创建一个测试类,不能直接调用,且类必须继承unittest下面的TestCase """docstring for Mytest""" def test_add(self): #创建测试方法,测试方法必须以test开头 c = test.Calculator() result = c.add(3,5) print (result) self.assertEqual(result ,9,msg = ‘error‘) #断言方法用unittest自带的,如果要使用父类下面的方法的话需要self.方法 if __name__ == ‘__main__‘: #unittest中用main函数来执行case unittest.main()
3、steUp()与tearDown()的使用
3.1、普通的单元case
import test import unittest class MyTest(unittest.TestCase): """docstring for Mytest""" def test_add(self): c = test.Calculator() result = c.add(3,5) print (result) self.assertEqual(result ,8,msg = ‘1‘) def test_add2(self): c = test.Calculator() result = c.add(30,50) print (result) self.assertEqual(result ,80,msg = ‘2‘) def test_sub(self): c = test.Calculator() result = c.sub(3,5) print (result) self.assertEqual(result ,-2,msg = ‘3‘) def test_sub2(self): c = test.Calculator() result = c.sub(30,50) print (result) self.assertEqual(result ,-20,msg = ‘4‘) if __name__ == ‘__main__‘: unittest.main()
3.2、使用setUp()与tearDown()
setUp():每条用例执行之前初始化一些数据
tearDown():每条用例执行之后做一些关闭的动作
import test import unittest class MyTest(unittest.TestCase): def setUp(self): print("start test.") def tearDown(self): print("end test.") """docstring for Mytest""" def test_add(self): c = test.Calculator() result = c.add(3,5) print (result) self.assertEqual(result ,8,msg = ‘1‘) def test_add2(self): c = test.Calculator() result = c.add(30,50) print (result) self.assertEqual(result ,80,msg = ‘2‘) def test_sub(self): c = test.Calculator() result = c.sub(3,5) print (result) self.assertEqual(result ,-2,msg = ‘3‘) def test_sub2(self): c = test.Calculator() result = c.sub(30,50) print (result) self.assertEqual(result ,-20,msg = ‘4‘) if __name__ == ‘__main__‘: unittest.main() #执行结果如下: #每条case执行都会执行setUp()与tearDown()方法,所以会有4条 # start test. # 8 # ....end test. # start test. # 80 # end test. # start test. # -2 # end test. # start test. # -20 # end test. # ---------------------------------------------------------------------- # Ran 4 tests in 0.000s # OK # [Finished in 0.4s]
可将上面c = test.Calculator() 共有的语句添加到setUp()中去
import test import unittest class MyTest(unittest.TestCase): def setUp(self): #如果直接是c = test.Calculator()的话,c表示的是setUp私有的变量,不能被下面的case所调用,所以需要在前面加self.c变成共有的 self.c = test.Calculator() def tearDown(self): print("end test.") """docstring for Mytest""" def test_add(self): result = self.c.add(3,5) #使用c也需要是self.c使用公有的 print (result) self.assertEqual(result ,8,msg = ‘1‘) def test_add2(self): result = self.c.add(30,50) print (result) self.assertEqual(result ,80,msg = ‘2‘) def test_sub(self): result = self.c.sub(3,5) print (result) self.assertEqual(result ,-2,msg = ‘3‘) def test_sub2(self): result = self.c.sub(30,50) print (result) self.assertEqual(result ,-20,msg = ‘4‘) if __name__ == ‘__main__‘: unittest.main() #运行结果 # 8 # end test. # .... # ---------------------------------------------------------------------- # Ran 4 tests in 0.000s # OK # 80 # end test. # -2 # end test. # -20 # end test. # [Finished in 0.4s]
3.3、执行特定的用例
- 测试套件:装载要运行测试用例的集合TestSuite()下面的addTest方法
- 测试运行:TextTestRunner()下面的run()方法
- 往测试套件里面加新的测试用例
import test import unittest class MyTest(unittest.TestCase): def setUp(self): self.c = test.Calculator() def tearDown(self): print("end test.") """docstring for Mytest""" def test_add(self): result = self.c.add(3,5) print (result) self.assertEqual(result ,8,msg = ‘1‘) def test_add2(self): result = self.c.add(30,50) print (result) self.assertEqual(result ,80,msg = ‘2‘) def test_sub(self): result = self.c.sub(3,5) print (result) self.assertEqual(result ,-2,msg = ‘3‘) def test_sub2(self): result = self.c.sub(30,50) print (result) self.assertEqual(result ,-20,msg = ‘4‘) if __name__ == ‘__main__‘: # 创建测试套件 suit = unittest.TestSuite() suit.addTest(MyTest("test_sub2")) suit.addTest(MyTest("test_sub")) #测试运行 runner = unittest.TextTestRunner() runner.run(suit) #运行结果 # -20 # end test. # .. # ---------------------------------------------------------------------- # Ran 2 tests in 0.000s # OK # -2 # end test. # [Finished in 0.4s]
3.4、运行的依据
类下面有继承TestCase()方法
用例是test开头的命名
执行顺序:test_aad2 先于test_add2,依据ASCII排序来决定执行顺序