python中使用 unittest.TestCase單元測試的用例詳解
python標(biāo)準(zhǔn)庫中的模塊unittest提供了代碼測試工具。單元測試用于核實(shí)函數(shù)的莫個(gè)方面沒有問題;測試用例是一組單元測試,這些單元測試一起核實(shí)函數(shù)在各種情形下的行為都符合要求。良好的測試用例考慮到了函數(shù)可能收到的各種輸入,包含針對所有這些情形的測試。全覆蓋測試用例包含一整套單元測試,涵蓋了各種可能的函數(shù)使用方式。對于大型項(xiàng)目,要實(shí)現(xiàn)全覆蓋可能很難,通常,最初只要針對代碼的重要行為編寫測試即可,等項(xiàng)目被廣泛使用時(shí)再考慮全覆蓋。
各種斷言方法python 在unittest.TestCase 中提高了很多斷言方法。
unittest Module中的斷言方法
方法 用途 assertEqual(a,b) 核實(shí)a == b assertNotEqual(a,b) 核實(shí)a != b assertTrue(x) 核實(shí)x為True assertFalse(x) 核實(shí)x為False assertIn(item,list) 核實(shí)ietm在list中 assertNotIn(item,list) 核實(shí)item不在list中 函數(shù)測試 1.準(zhǔn)備測試函數(shù)name_function.py
def get_formatted_name(first, last): ’’’生成整潔的姓名’’’ full_name = first + ’ ’ + last return full_name.title()2.編寫一個(gè)能使用它的程序
nams.py
from name_function import get_formatted_nameprint('Enter ’q’ at any time to quit.')while True: first = input('nPlease give me a first name: ') if first == ’q’:break last = input('Please give me a last name: ') if last == ’q’:break formatted_name = get_formatted_name(first, last) print('tNeatly formatted name: ' + formatted_name + ’.’)3.對函數(shù)進(jìn)行單元測試
test_name_function.py
import unittestfrom unittest import TestCasefrom name_function import get_formatted_nameclass NamesTestCase(TestCase): ’’’測試name_function.py’’’ def test_first_last_name(self):’’’能夠正確地處理象 Janis Joplin這樣的姓名嗎?’’’formtted_name = get_formatted_name(’janis’, ’joplin’)self.assertEqual(formtted_name, ’Janis Joplin’)# 執(zhí)行unittest.main()
python test_name_function.py
第一行的句點(diǎn) 表示測試通過了,接下來的一行指出python運(yùn)行了一個(gè)測試,消耗的時(shí)間不到0.001秒,最后的OK表示改測試用例中的所有測試單元都通過了。
類測試1.準(zhǔn)備測試的類survey.py
class AnonmousSurvey(): '''收集匿名調(diào)查問卷的答案''' def __init__(self, question):'''存儲(chǔ)一個(gè)問題,并為存儲(chǔ)答案做準(zhǔn)備'''self.question = questionself.responses = [] def show_question(self):'''顯示調(diào)查問卷'''print(self.question) def store_response(self, new_response):'''存儲(chǔ)單份調(diào)查答卷'''self.responses.append(new_response) def show_results(self):'''顯示收集到的所有答卷'''print('Survey results')for response in self.responses: print(’- ’ + response)2.編寫一個(gè)能使用它的程序
language_survey.py
from survey import AnonmousSurvey# 定義一個(gè)問題,并創(chuàng)建一個(gè)表示調(diào)查的AnonymousSurvey對象question = 'What language did you first learn to speak?'my_survey = AnonmousSurvey(question)# 顯示問題并存儲(chǔ)答案my_survey.show_question()print('Enter ’q’ at any time to quit.n')while True: response = input('Language: ') if response == ’q’:break my_survey.store_response(response)# 顯示調(diào)查結(jié)果print('nThank you to everyoune who participated in the survey!')my_survey.show_results()3.對類進(jìn)行單元測試
import unittestfrom survey import AnonmousSurveyclass TestAnonmousSurvey(unittest.TestCase): '''針對AnonymousSurvey類的測試''' def test_store_single_response(self):'''測試單個(gè)答案會(huì)被妥善地存儲(chǔ)'''question = 'What language did you first learn to speak?'my_survey = AnonmousSurvey(question)my_survey.store_response(’English’)self.assertIn(’English’, my_survey.responses) def test_store_three_responses(self):'''測試多個(gè)答案是否會(huì)被存儲(chǔ)'''question = 'What language did you first learn to speak?'my_survey = AnonmousSurvey(question)responses = ['English', 'Chinses', 'Japan']for response in responses: my_survey.store_response(response)for response in responses: self.assertIn(response, my_survey.responses)unittest.main()
可以看到對類的單元測試也是成功的。雖然成功了,但是做法不是很好,測試有些重復(fù)了,下面使用unittest的另一項(xiàng)功能來提高它們的效率
方法 setUP()
如果你在TestCase類中包含方法setUP(),python將先運(yùn)行它,在運(yùn)行各個(gè)以test_開頭的方法。
test_survey_setup.py
import unittestfrom survey import AnonmousSurveyclass TestAnonmousSurvey(unittest.TestCase): '''針對AnonymousSurvey類的測試''' def setUp(self):'''創(chuàng)建一個(gè)調(diào)查對象和一組答案,供使用的測試方法使用'''question = 'What language did you first learn to speak?'self.my_survey = AnonmousSurvey(question)self.responses = ['English', 'Chinses', 'Japan'] def test_store_single_response(self):'''測試單個(gè)答案會(huì)被妥善地存儲(chǔ)'''self.my_survey.store_response(self.responses[0])self.assertIn(self.responses[0], self.my_survey.responses) def test_store_three_responses(self):'''測試多個(gè)答案是否會(huì)被存儲(chǔ)'''for response in self.responses: self.my_survey.store_response(response)for response in self.responses: self.assertIn(response, self.my_survey.responses)unittest.main()
測試自己編寫的類時(shí),方法setUP()讓測試方法編寫起來更容易:可以在setUP()方法中創(chuàng)建一系列實(shí)例并設(shè)置它們的屬性,再在測試方法中直接使用這些實(shí)例。相比于在每個(gè)測試方法中都創(chuàng)建實(shí)例并設(shè)置屬性,這要容易的多。
注意運(yùn)行測試用例時(shí),每完成一個(gè)單元測試,python都打印一個(gè)字符: 測試通過時(shí)打印一個(gè)句點(diǎn); 測試引發(fā)錯(cuò)誤時(shí)打印一個(gè)E; 測試導(dǎo)致斷言失敗時(shí)打印一個(gè)F。這就是運(yùn)行測試用例時(shí),在輸出的第一行中看到的句點(diǎn)和字符數(shù)量各不相同的原因。如果測試用例包含很多單元測試,需要運(yùn)行很長時(shí)間,就可以通過觀察這些結(jié)果來獲悉有多少個(gè)測試通過了。
到此這篇關(guān)于python中使用 unittest.TestCase 進(jìn)行單元測試的文章就介紹到這了,更多相關(guān)python單元測試內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 如何利用python操作注冊表2. Xml簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理3. jsp文件下載功能實(shí)現(xiàn)代碼4. 詳解瀏覽器的緩存機(jī)制5. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享6. jsp實(shí)現(xiàn)登錄驗(yàn)證的過濾器7. phpstudy apache開啟ssi使用詳解8. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令9. 如何在jsp界面中插入圖片10. JSP之表單提交get和post的區(qū)別詳解及實(shí)例
