25 【python入門指南】如何編寫測試代碼
python如何編寫測試代碼
python內置了unittest,使得寫應用層的單元測試變得超乎尋常的簡單。
1,執行單個測試函數
#!/bin/python import unittest class TestMathFunc(unittest.TestCase): def test_add(self): self.assertEqual(3, 1+2) self.assertEqual(4, 2+2) self.assertNotEqual(3, 1+3) def runTest(self): self.test_add() suite= unittest.TestSuite() testCase = [TestMathFunc()] suite.addTests(testCase) runner = unittest.TextTestRunner(verbosity = 2) runner.run(suite)
單元測試,只需要仿照上面的例子寫就可以完成自己的測試代碼。
其中包含幾個部分:
a,測試類(要繼承unittest.TestCase);
b,測試小組件,unittest.TestSuite,負責將測試類裝入進來;
c,執行類,unittest.TextTestRunner;
2,執行多個測試函數
#!/bin/python import unittest class TestMathFunc(unittest.TestCase): def test_add(self): self.assertEqual(3, 1+2) self.assertEqual(4, 2+2) self.assertNotEqual(3, 1+3) def test_minus(self): self.assertEqual(-1, 1-2) def runTest(self): self.test_add() self.test_minus() suite= unittest.TestSuite() #testCase = [TestMathFunc(‘test_add‘), TestMathFunc(‘test_minus‘)] testCase = [TestMathFunc()] suite.addTests(testCase) runner = unittest.TextTestRunner(verbosity = 2) runner.run(suite)
修改runTest函數,將測試函數包含進來即可。
3,執行多個測試類
#!/bin/python import unittest class TestStringFunc(unittest.TestCase): def test_str_concat(self): self.assertEqual("abcabc", "abc" + "abc") def runTest(self): self.test_str_concat() class TestMathFunc(unittest.TestCase): def test_add(self): self.assertEqual(3, 1+2) self.assertEqual(4, 2+2) self.assertNotEqual(3, 1+3) def test_minus(self): self.assertEqual(-1, 1-2) def runTest(self): self.test_add() self.test_minus() suite = unittest.TestSuite() testCase = [TestMathFunc(), TestStringFunc()] suite.addTests(testCase) runner = unittest.TextTestRunner(verbosity = 2) runner.run(suite)
在suite添加多個測試類,即可。
當然還有另外的方式:
... suite = unittest.TestSuite() testCase = [TestMathFunc()] suite.addTests(testCase) testCase2 = TestStringFunc() suite.addTest(testCase2) runner = unittest.TextTestRunner(verbosity = 2) runner.run(suite)
通過以上代碼片段,即可實現,自主地控制多個測試類,自主地控制多個函數執行測試,十分方便。
4,有時候我們不想每個測試函數都顯示調用,而是加載所有test_開頭的測試函數,怎麽做?
構建兩個文件:t3.py,main.py
main文件存放執行代碼,t3文件僅存放測試類,不存放任何執行代碼
main.py
#!/bin/python
#main.py
import unittest from t3 import TestMathFunc, TestStringFunc suite2 = unittest.TestLoader().discover(‘.‘, ‘t3.py‘) runner = unittest.TextTestRunner(verbosity = 2) runner.run(suite2)
t3.py
#!/bin/python #t3.py import unittest class TestStringFunc(unittest.TestCase): def test_str_concat(self): self.assertEqual("abcabc", "abc" + "abc") # def runTest(self): # self.test_str_concat() class TestMathFunc(unittest.TestCase): def test_add(self): self.assertEqual(3, 1+2) self.assertEqual(4, 2+2) self.assertNotEqual(3, 1+3) def test_minus(self): self.assertEqual(-1, 1-2) # def runTest(self): # self.test_add() # self.test_minus()
執行main.py,輸出結果:
test_add (t3.TestMathFunc) ... ok test_minus (t3.TestMathFunc) ... ok test_str_concat (t3.TestStringFunc) ... ok ---------------------------------------------------------------------- Ran 3 tests in 0.001s
通過 unittest.TestLoader().discover(‘.‘, ‘t3.py‘) 將t3文件中包含的所有測試類導入到suite中。
依賴這個功能,我們可以將所有的需要測試的文件,導入到main主函數中,進行集中管理和測試。註意:這裏不再依賴runTest函數,只要函數名中包含test前綴就會被包含進來。
5,有時候測試用例需要提供上下文環境,怎麽做?
我們針對例子4,僅修改t3.py測試類。
class TestMathFunc(unittest.TestCase): def setUp(self): print("\nTestMathFunc:setup") self.a = 1 self.b = 2 def test_add(self): self.assertEqual(3, 1+2) self.assertEqual(4, 2+2) self.assertNotEqual(3, 1+3) self.assertEqual(3, self.a + self.b) def test_minus(self): self.assertEqual(-1, 1-2)
這裏我們準備了環境信息(準備兩個變量a和b),使用這兩個變量進行單元測試。
test_add (t3.TestMathFunc) ... TestMathFunc:setup ok test_minus (t3.TestMathFunc) ... TestMathFunc:setup ok test_str_concat (t3.TestStringFunc) ... ok ---------------------------------------------------------------------- Ran 3 tests in 0.002s OK
函數的輸出如上。
類的單元測試,每個函數執行之前都會執行setUp函數。
對應的是tearDown,用來銷毀構建的環境信息。可以自己嘗試下。
參考網站:
更詳細的解釋:https://www.jianshu.com/p/38948d0d73f5
最詳細的官方文檔:https://docs.python.org/3/library/unittest.html#module-unittest
25 【python入門指南】如何編寫測試代碼