什么是單元測試
單元測試是用來對一個模塊、一個函數或者一個類來進行正確性檢驗的測試工作。
比如對于函數abs(),我們可以編寫的測試用例為:
(1)輸入正數,比如1、1.2、0.99,期待返回值與輸入相同
(2)輸入復數,比如-1、-1.2、-0.99,期待返回值與輸入相反
(3)輸入0,期待返回0
(4)輸入非數值類型,比如None、[]、{}、期待拋出TypeError
把上面這些測試用例放到一個測試模塊里,就是一個完整的單元測試
unittest工作原理
unittest中最核心的四部分是:TestCase,TestSuite,TestRunner,TestFixture
(1)一個TestCase的實例就是一個測試用例。測試用例就是指一個完整的測試流程,包括測試前準備環境的搭建(setUp),執行測試代碼(run),以及測試后環境的還原(tearDown)。元測試(unit test)的本質也就在這里,一個測試用例是一個完整的測試單元,通過運行這個測試單元,可以對某一個問題進行驗證。
(2)而多個測試用例集合在一起,就是TestSuite,而且TestSuite也可以嵌套TestSuite。
(3)TestLoader是用來加載TestCase到TestSuite中的。
(4)TextTestRunner是來執行測試用例的,其中的run(test)會執行TestSuite/TestCase中的run(result)方法
(5)測試的結果會保存到TextTestResult實例中,包括運行了多少測試用例,成功了多少,失敗了多少等信息。
綜上,整個流程就是首先要寫好TestCase,然后由TestLoader加載TestCase到TestSuite,然后由TextTestRunner來運行TestSuite,運行的結果保存在TextTestResult中,整個過程集成在unittest.main模塊中。
python unittest簡介
unittest是python下的單元測試框架,是java JUnit的python版本, 跟其它語言下的單元測試框架風格類似,unittest支持自動化測試、共享setup和teardown代碼、測試聚合成集、獨立于報告框架。unittest模塊提供了一個豐富的工具集用于構建和執行用例,先看一個入門的例子:
import unittestclass TestStringMethods(unittest.TestCase):def test_upper(self):self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2)if __name__ == '__main__': unittest.main()可以通過繼承unittest.TestCase創建一個測試用例TestStringMethods,在這個用例中定義了測試函數,這些函數名字都以”test”開頭,在執行測試用例TestStringMethods時,這些方法會被自動調用。每個測試函數中都調用了assertTrue()和assertFalse()方法檢查預期結果,或者使用assertRaises()確認產生了一個特定異常。現在來看一下這段代碼的運行結果:
...
----------------------------------------------------------------------
Ran 3 tests in 0.000sOK
有時我們需要在用例執行前后做一些工作如初始化和清理,這就需要實現setUp()和tearDown()方法
import unittestclass WidgetTestCase(unittest.TestCase): def setUp(self): print("setUp()") def test_1(self): print("test_1") def test_2(self): print("test_2") def tearDown(self): print("tearDown()")if __name__ == '__main__': unittest.main()運行結果:
setUp()
.test_1
tearDown()
setUp()
.test_2
tearDown()
----------------------------------------------------------------------
Ran 2 tests in 0.000sOK
注:如果setUp()執行成功(沒有異常發生),那么無論測試方法是否通過,tearDown()都會被執行
根據所測的特性測試用例被組合在一起,通過調用unittest.main(),unittest測試框架會自動收集所有模塊的測試用例然后執行。
import unittestclass WidgetTestCase(unittest.TestCase): def setUp(self): print("WidgetTestCase setUp()") def test_Widget(self): print("test_Widget") def tearDown(self): print("WidgetTestCase tearDown()")class FuncTestCase(unittest.TestCase): def setUp(self): print("FuncTestCase setUp()") def test_func(self): print("test_func") def tearDown(self): print("FuncTestCase tearDown()")if __name__ == '__main__': unittest.main()運行結果:
FuncTestCase setUp()
test_func
FuncTestCase tearDown()
.WidgetTestCase setUp()
test_Widget
WidgetTestCase tearDown()
.
----------------------------------------------------------------------
Ran 2 tests in 0.003sOK
如果想構建自已的用例集,只需要這么做:
import unittestclass WidgetTestCase(unittest.TestCase): def setUp(self): print("WidgetTestCase setUp()") def test_Widget(self): print("test_Widget") def tearDown(self): print("WidgetTestCase tearDown()")class FuncTestCase(unittest.TestCase): def setUp(self): print("FuncTestCase setUp()") def test_func(self): print("test_func") def tearDown(self): print("FuncTestCase tearDown()")def suite(): suite = unittest.TestSuite() suite.addTest(FuncTestCase('test_func')) return suiteif __name__ == '__main__': runner=unittest.TextTestRunner() runner.run(suite())運行結果:
FuncTestCase setUp()
test_func
FuncTestCase tearDown()
.
----------------------------------------------------------------------
Ran 1 test in 0.001sOK
unittest中相關類和函數
在unittest中 TestCase類的實例代表邏輯測試單元,這個類通常被當作測試類的基類使用, TestCase類實現了許多測試相關的接口,主要是以下三組方法:
1.執行測試用例的方法
setUp()#在每個測試方法之前執行,這個方法引發的異常會被認為是錯誤,而非測試失敗,默認實現是不做任何事tearDown()#在每個測試方法之后執行,即使測試方法拋出異常tearDown()方法仍會執行,并且只有setUp()成功執行時tearDown()才會執行,#同樣這個方法引發的異常會被認為是錯誤,而非測試失敗。默認實現是不做任何事setUpClass()#在一個測試類的所有測試方法執行之前執行,相當于google test中的SetUpTestCase()方法,setUpClass()必須被裝飾成一個classmethod()@classmethoddef setUpClass(cls): ...tearDownClass()#在一個測試類的所有測試方法執行之后執行,相當于google test中的TearDownTestCase()方法,tearDownClass()必須被裝飾成一個classmethod()@classmethoddef tearDownClass(cls): ...
2.檢查條件和報告錯誤的方法
| Method | Checks that | New in |
|---|---|---|
| assertEqual(a, b) | a == b | |
| assertNotEqual(a, b) | a != b | |
| assertTrue(x) | bool(x) is True | |
| assertFalse(x) | bool(x) is False | |
| assertIs(a, b) | a is b | 3.1 |
| assertIsNot(a, b) | a is not b | 3.1 |
| assertIsNone(x) | x is None | 3.1 |
| assertIsNotNone(x) | x is not None | 3.1 |
| assertIn(a, b) | a in b | 3.1 |
| assertNotIn(a, b) | a not in b | 3.1 |
| assertIsInstance(a, b) | isinstance(a, b) | 3.2 |
| assertNotIsInstance(a, b) | not isinstance(a, b) | 3.2 |
| assertRaises(exc, fun, *args, **kwds) | fun(*args, **kwds) raises exc | |
| assertRaisesRegex(exc, r, fun, *args, **kwds) | fun(*args, **kwds) raises exc and the message matches regex r | 3.1 |
| assertWarns(warn, fun, *args, **kwds) | fun(*args, **kwds) raises warn | 3.2 |
| assertNotAlmostEqual(a, b) | round(a-b, 7) != 0 | |
| assertGreater(a, b) | a > b | 3.1 |
| assertGreaterEqual(a, b) | a >= b | 3.1 |
| assertLess(a, b) | a < b | 3.1 |
| assertLessEqual(a, b) | a <= b | 3.1 |
| assertRegex(s, r) | r.search(s) | 3.1 |
| assertNotRegex(s, r) | not r.search(s) | 3.2 |
| assertCountEqual(a, b) | a and b have the same elements in the same number, regardless of their order | 3.2 |
| assertWarnsRegex(warn, r, fun, *args, **kwds) | fun(*args, **kwds) raises warn and the message matches regex r | 3.2 |
| assertLogs(logger, level) | The with block logs on logger with minimum level | 3.4 |
| assertMultiLineEqual(a, b) | strings | 3.1 |
| assertSequenceEqual(a, b) | sequences | 3.1 |
| assertListEqual(a, b) | lists | 3.1 |
| assertTupleEqual(a, b) | tuples | 3.1 |
| assertSetEqual(a, b) | sets or frozensets | 3.1 |
| assertDictEqual(a, b) | dicts | 3.1 |
新聞熱點
疑難解答