一、生成Django項目中--sign文件--tests.py文件中爲Django的測試case管理文件python
Django下面的測試模塊(上圖中的TestCase)是繼承python下面的unit test(單元測試框架)下面的TestCase模塊框架
二、在Python中建立的全部類默認都是繼承object函數
class Calculator: class Calculator(): class Calculator(object):
class Calculator(object): def add(self,a,b): return a + b def sub(self,a ,b): return a - b
2.2.一、不用unittest框架實現單元測試
count_test.py文件測試
import test c = test.Calculator() #引入test文件中的Calculator類 result = c.add(3,5) #調用Calculator類下面的add方法 print (result)
驗證利用assert方法進行驗證明際結果與預期結果知否一致ui
import test c = test.Calculator() result = c.add(3,5) print (result) assert result == 8
2.2.二、用unittest框架實現spa
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.三、caseblog
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.一、普通的單元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.二、使用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.三、執行特定的用例
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.四、運行的依據
類下面有繼承TestCase()方法
用例是test開頭的命名
執行順序:test_aad2 先於test_add2,依據ASCII排序來決定執行順序