Python單元測試框架unittest使用方法講解
概述
1.測試腳手架(test fixture)
測試準(zhǔn)備前要做的工作和測試執(zhí)行完后要做的工作.包括setUp()和tearDown().
2.測試案例(test case)
最小的測試單元.
3.測試套件(test suite)
測試案例的集合.
4.測試運(yùn)行器(test runner)
測試執(zhí)行的組件.
命令行接口
可以用命令行運(yùn)行測試模塊,測試類以及測試方法.
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
可加-v打印詳細(xì)信息
python -m unittest -v test_module
測試案例自動(dòng)搜索
unittest支持簡單的test discovery. 命令行傳入discovery后,框架會(huì)自動(dòng)在當(dāng)前目錄搜索要測試的案例并執(zhí)行.搜索目錄必須是包或者模塊.基本使用如下:
cd project_directory
python -m unittest discover
子選項(xiàng)如下:
-v, –verbose
輸出信息的詳細(xì)級別
-s, –start-directory directory
開始搜索目錄 (默認(rèn)為當(dāng)前目錄)
-p, –pattern pattern
匹配的文件名 (默認(rèn)為test*.py)
-t, –top-level-directory directory
搜索的頂層目錄 (默認(rèn)為start directory)
創(chuàng)建測試代碼
1.方式一
創(chuàng)建子類繼承unittest.TestCase,然后重寫以下方法
class WidgetTestCase(unittest.TestCase):
def setUp(self):
pass
def runTest(self):
pass
def tearDown(self):
pass
運(yùn)行
2.方式二
編寫以test開頭的方法
class WidgetTestCase(unittest.TestCase):
def setUp(self):
pass
def test_xx1(self)
def test_xx2(self)
...
def test_xxN(self)
def tearDown(self):
pass
構(gòu)建測試套件
方式一
widgetTestSuite = unittest.TestSuite()
widgetTestSuite.addTest(WidgetTestCase('test_default_size'))
widgetTestSuite.addTest(WidgetTestCase('test_resize'))
方式二(推薦)
def suite():
suite = unittest.TestSuite()
suite.addTest(WidgetTestCase('test_default_size'))
suite.addTest(WidgetTestCase('test_resize'))
return suite
方式三(推薦)
def suite():
tests = ['test_default_size', 'test_resize']
return unittest.TestSuite(map(WidgetTestCase, tests))
方式四
多個(gè)測試套件構(gòu)建成更大的測試套件
suite1 = module1.TheTestSuite()
suite2 = module2.TheTestSuite()
alltests = unittest.TestSuite([suite1, suite2])
方式五
unittest的TestLoader提供生成默認(rèn)的測試套件
suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)
忽略測試案例( Python2.7支持)
可以分無條件忽略和有條件忽略,通過裝飾器實(shí)現(xiàn)
class MyTestCase(unittest.TestCase):
@unittest.skip("demonstrating skipping")
def test_nothing(self):
self.fail("shouldn't happen")
@unittest.skipIf(mylib.__version__ < (1, 3),
"not supported in this library version")
def test_format(self):
# Tests that work for only a certain version of the library.
pass
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_windows_support(self):
# windows specific testing code
pass
測試類也可以忽略
@unittest.skip("showing class skipping")
class MySkippedTestCase(unittest.TestCase):
def test_not_run(self):
pass
- python單元測試unittest實(shí)例詳解
- Python中unittest用法實(shí)例
- Python+request+unittest實(shí)現(xiàn)接口測試框架集成實(shí)例
- Python Unittest自動(dòng)化單元測試框架詳解
- Python unittest 簡單實(shí)現(xiàn)參數(shù)化的方法
- Python unittest單元測試框架的使用
- Python中Unittest框架的具體使用
- Python單元測試框架unittest簡明使用實(shí)例
- Python Unittest根據(jù)不同測試環(huán)境跳過用例的方法
- 一篇文章搞懂Python Unittest測試方法的執(zhí)行順序
相關(guān)文章
python自動(dòng)化神器pyautogui使用步驟
這篇文章主要給大家介紹了關(guān)于python自動(dòng)化神器pyautogui使用步驟的相關(guān)資料,在Python當(dāng)中不僅代碼簡單,而且有著非常豐富的模塊,pyautogui就可以稱之為自動(dòng)化操作的"神器",需要的朋友可以參考下2023-07-07Python實(shí)現(xiàn)刪除重復(fù)視頻文件的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)刪除重復(fù)視頻文件功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下2022-10-10python 獲取指定文件夾下所有文件名稱并寫入列表的實(shí)例
下面小編就為大家分享一篇python 獲取指定文件夾下所有文件名稱并寫入列表的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python實(shí)現(xiàn)24點(diǎn)小游戲
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)24點(diǎn)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Yolov5訓(xùn)練意外中斷后如何接續(xù)訓(xùn)練詳解
目標(biāo)檢測是計(jì)算機(jī)視覺上的一個(gè)重要任務(wù),下面這篇文章主要給大家介紹了關(guān)于Yolov5訓(xùn)練意外中斷后如何接續(xù)訓(xùn)練的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03Python使用Keras OCR實(shí)現(xiàn)從圖像中刪除文本
這篇文章主要為大家介紹了如何在Python中利用Keras OCR實(shí)現(xiàn)快速地從圖像中刪除文本,從而作為圖像分類器的預(yù)處理步驟,需要的可以參考一下2022-03-03python 子類調(diào)用父類的構(gòu)造函數(shù)實(shí)例
這篇文章主要介紹了python 子類調(diào)用父類的構(gòu)造函數(shù)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Win10系統(tǒng)下安裝編輯器之神(The?God?of?Editor)Vim并且構(gòu)建Python生態(tài)開發(fā)環(huán)境過程(2
這篇文章主要介紹了Win10系統(tǒng)下安裝編輯器之神(The?God?of?Editor)Vim并且構(gòu)建Python生態(tài)開發(fā)環(huán)境(2020年最新攻略),本次我們在Win10平臺(tái)構(gòu)建一套以Vim為核心的Python開發(fā)環(huán)境,需要的朋友可以參考下2023-01-01