Python unittest模塊用法實(shí)例分析
本文實(shí)例講述了Python unittest模塊用法。分享給大家供大家參考,具體如下:
python的unittest模塊提供了一個(gè)測試框架,只要我們寫一個(gè)繼承unittest.TestCase
的類,類中用setUp做初始化,用tearDown
做清理。
主要用到的函數(shù)有:
failedinfo表示不成立打印信息failedinfo,為可選參數(shù)
self.fail([msg])會(huì)無條件的導(dǎo)致測試失敗,不推薦使用。
self.assertEqual(value1, value2, failedinfo) # 斷言value1 == value2
self.assertTrue(表達(dá)式, failedinfo) # 斷言value為真
self.assertFalse(表達(dá)式, failedinfo) # 斷言value為假
# 斷言肯定發(fā)生異常,如果沒發(fā)生異常,則為測試失敗。
# 參數(shù)1為異常,參數(shù)二為拋出異常的調(diào)用對象,剩余參數(shù)為傳遞給可調(diào)用對象的參數(shù)。
self.assertRaises(ValueError, self.widget.resize, -1, -1)
調(diào)用時(shí)機(jī)的加self,如self.assertEqual(self.seq, range(10)),self.assertTrue(value > 100)
更詳細(xì)的教程見:http://pyunit.sourceforge.net/pyunit_cn.html
Python代碼:
#coding=utf-8 import random import unittest class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.seq = 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, 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) results_fields = [ ("username", unicode), ("showid", unicode), ("total_pv", int), ("pubdate", unicode), ("tags", list), ("showname", unicode), ("pg", int), ("ext", str), ] results_fields_map = dict(results_fields) class TestDictValueFormatFunctions(unittest.TestCase): def setUp(self): self.results = [{ "username": u"瘋狂豆花", "showid": u"130e28f0fe0811e0a046", "total_pv": 14503214, "pubdate": u"2012-07-07 01:22:47", "tags": [ "軒轅劍", "天之痕" ], "showname" : u"軒轅劍之天之痕", "pg" : 1, "ext" : "mp4" } ] def test_format(self): self.assertTrue(isinstance(self.results, list), "self.results's type must be dict but got {0}".format(type(self.results))) for r in self.results: for f in results_fields_map: value = r.get(f, None) self.assertTrue(isinstance(value, results_fields_map[f]), u"{0}'s type must be {1} but got {2}".format(value, results_fields_map[f], type(value))) #self.assertTrue(isinstance(value, results_fields_map[f])) def test_value(self): for r in self.results: self.assertEqual(r["pg"], 1) self.assertEqual(r["ext"], u"mp4") if __name__ == '__main__': # unittest.main() # 用這個(gè)是最簡單的,下面的用法可以同時(shí)測試多個(gè)類 # unittest.TextTestRunner(verbosity=2).run(suite1) # 這個(gè)等價(jià)于上述但可設(shè)置verbosity=2,省去了運(yùn)行時(shí)加-v suite1 = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions) suite2 = unittest.TestLoader().loadTestsFromTestCase(TestDictValueFormatFunctions) suite = unittest.TestSuite([suite1, suite2]) unittest.TextTestRunner(verbosity=2).run(suite)
運(yùn)行結(jié)果:
test_choice (__main__.TestSequenceFunctions) ... ok
test_sample (__main__.TestSequenceFunctions) ... ok
test_shuffle (__main__.TestSequenceFunctions) ... ok
test_format (__main__.TestDictValueFormatFunctions) ... ok
test_value (__main__.TestDictValueFormatFunctions) ... ok----------------------------------------------------------------------
Ran 5 tests in 0.013sOK
更多Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python入門與進(jìn)階經(jīng)典教程》、《Python字符串操作技巧匯總》、《Python列表(list)操作技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
- python unittest實(shí)現(xiàn)api自動(dòng)化測試
- Python Unittest自動(dòng)化單元測試框架詳解
- python如何使用unittest測試接口
- Python+request+unittest實(shí)現(xiàn)接口測試框架集成實(shí)例
- 詳解Python之unittest單元測試代碼
- python的unittest測試類代碼實(shí)例
- 深入理解Python單元測試unittest的使用示例
- python+requests+unittest API接口測試實(shí)例(詳解)
- 利用Python中unittest實(shí)現(xiàn)簡單的單元測試實(shí)例詳解
- Python中unittest模塊做UT(單元測試)使用實(shí)例
- python單元測試unittest實(shí)例詳解
- 詳解python單元測試框架unittest
相關(guān)文章
Python基于React-Dropzone實(shí)現(xiàn)上傳組件的示例代碼
本文主要介紹了在React-Flask框架上開發(fā)上傳組件的技巧。文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08TensorFlow人工智能學(xué)習(xí)創(chuàng)建數(shù)據(jù)實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了TensorFlow人工智能學(xué)習(xí)創(chuàng)建數(shù)據(jù)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11python正則表達(dá)式完成車牌號檢驗(yàn)的代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于python正則表達(dá)式完成車牌號檢驗(yàn)的相關(guān)資料,在Python中正則表達(dá)式是一種用于匹配和操作字符串的強(qiáng)大工具,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02python 進(jìn)階學(xué)習(xí)之python裝飾器小結(jié)
這篇文章主要介紹了python 進(jìn)階學(xué)習(xí)之python裝飾器小結(jié),本文通過場景分析給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Python UnicodeEncodeError: ''gbk'' codec can''t encode chara
這篇文章主要介紹了Python UnicodeEncodeError: 'gbk' codec can't encode character 解決方法,需要的朋友可以參考下2015-04-04