Python中unittest用法實(shí)例
本文實(shí)例講述了Python中unittest的用法,分享給大家供大家參考。具體用法分析如下:
1. unittest module包含了編寫運(yùn)行unittest的功能,自定義的test class都要集成unitest.TestCase類,test method要以test開頭,運(yùn)行順序根據(jù)test method的名字排序,特殊方法:
① setup():每個(gè)測試函數(shù)運(yùn)行前運(yùn)行
② teardown():每個(gè)測試函數(shù)運(yùn)行完后執(zhí)行
③ setUpClass():必須使用@classmethod 裝飾器,所有test運(yùn)行前運(yùn)行一次
④ tearDownClass():必須使用@classmethod裝飾器,所有test運(yùn)行完后運(yùn)行一次
2. 示例代碼:
#文件名runtest.py import random import unittest class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.seq = list(range(10)) def test_shuffle(self): # make sure the shuffled sequence does not lose any elements random.shuffle(self.seq) self.seq.sort() self.assertEqual(self.seq, list(range(10))) # should raise an exception for an immutable sequence self.assertRaises(TypeError, random.shuffle, (1,2,3)) def test_choice(self): element = random.choice(self.seq) self.assertTrue(element in self.seq) def test_sample(self): with self.assertRaises(ValueError): random.sample(self.seq, 20) for element in random.sample(self.seq, 5): self.assertTrue(element in self.seq) if __name__ == '__main__': unittest.main()
3.運(yùn)行方式:在命令行直接運(yùn)行這個(gè)runtest.py
可以使用unitest.skip裝飾器族跳過test method或者test class,這些裝飾器包括:
① @unittest.skip(reason):無條件跳過測試,reason描述為什么跳過測試
② @unittest.skipif(conditition,reason):condititon為true時(shí)跳過測試
③ @unittest.skipunless(condition,reason):condition不是true時(shí)跳過測試
可以自定義skip decorator
#這是一個(gè)自定義的skip decorrator def skipUnlessHasattr(obj, attr): if hasattr(obj, attr): return lambda func: func return unittest.skip("{!r} doesn't have {!r}".format(obj, attr))
skip decorator示例代碼:
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
4.expected failure:使用@unittest.expectedFailure裝飾器,如果test失敗了,這個(gè)test不計(jì)入失敗的case數(shù)目
希望本文所述對大家Python程序設(shè)計(jì)的學(xué)習(xí)有所幫助。
- python單元測試unittest實(shí)例詳解
- Python單元測試框架unittest使用方法講解
- 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)文章
使用Dajngo 通過代碼添加xadmin用戶和權(quán)限(組)
這篇文章主要介紹了使用Dajngo 通過代碼添加xadmin用戶和權(quán)限(組),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python Pyqt5多線程更新UI代碼實(shí)例(防止界面卡死)
這篇文章通過代碼實(shí)例給大家介紹了Python Pyqt5多線程更新UI防止界面卡死的問題,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-12-12python+requests實(shí)現(xiàn)接口測試的完整步驟
這篇文章主要給大家介紹了關(guān)于python+requests實(shí)現(xiàn)接口測試的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10pandas數(shù)據(jù)清洗(缺失值和重復(fù)值的處理)
這篇文章主要介紹了pandas數(shù)據(jù)清洗(缺失值和重復(fù)值的處理),pandas對大數(shù)據(jù)有很多便捷的清洗用法,尤其針對缺失值和重復(fù)值,詳細(xì)介紹感興趣的小伙伴可以參考下面文章內(nèi)容2022-08-08Python實(shí)現(xiàn)的企業(yè)粉絲抽獎(jiǎng)功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的企業(yè)粉絲抽獎(jiǎng)功能,涉及Python數(shù)值運(yùn)算與隨機(jī)數(shù)生成相關(guān)操作技巧,需要的朋友可以參考下2019-07-07