Python unittest單元測試框架
阿新 • • 發佈:2018-12-22
一、python unittest簡介
unittest是python下的單元測試框架,是java JUnit的python版本, 跟其它語言下的單元測試框架風格類似,unittest支援自動化測試、共享setup和teardown程式碼、測試聚合成集、獨立於報告框架。unittest模組提供了一個豐富的工具集用於構建和執行用例,先看一個入門的例子
import unittest class 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.000s OK
有時我們需要在用例執行前後做一些工作如初始化和清理,這就需要實現setUp()和tearDown()方法
import unittest class 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.000s
OK
注:如果setUp()執行成功(沒有異常發生),那麼無論測試方法是否通過,tearDown()都會被執行
根據所測的特性測試用例被組合在一起,通過呼叫unittest.main(),unittest測試框架會自動收集所有模組的測試用例然後執行。
import unittest
class 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()
如果想構建自已的用例集,只需要這麼做:
import unittest
class 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 suite
if __name__ == '__main__':
runner=unittest.TextTestRunner()
runner.run(suite())
二、unittest中相關類和函式
在unittest中 TestCase類的例項代表邏輯測試單元,這個類通常被當作測試類的基類使用, TestCase類實現了許多測試相關的介面,主要是以下三組方法:
setUp()
#在每個測試方法之前執行,這個方法引發的異常會被認為是錯誤,而非測試失敗,預設實現是不做任何事
tearDown()
#在每個測試方法之後執行,即使測試方法丟擲異常tearDown()方法仍會執行,並且只有setUp()成功執行時tearDown()才會執行,
#同樣這個方法引發的異常會被認為是錯誤,而非測試失敗。預設實現是不做任何事
setUpClass()
#在一個測試類的所有測試方法執行之前執行,相當於google test中的SetUpTestCase()方法,setUpClass()必須被裝飾成一個classmethod()
@classmethod
def setUpClass(cls):
...
tearDownClass()
#在一個測試類的所有測試方法執行之後執行,相當於google test中的TearDownTestCase()方法,tearDownClass()必須被裝飾成一個classmethod()
@classmethod
def tearDownClass(cls):
...