色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

python中使用 unittest.TestCase單元測試的用例詳解

瀏覽:5日期:2022-08-06 18:44:45
目錄單元測試和測試用例各種斷言方法函數測試 1.準備測試函數2.編寫一個能使用它的程序3.對函數進行單元測試類測試1.準備測試的類2.編寫一個能使用它的程序3.對類進行單元測試注意單元測試和測試用例

python標準庫中的模塊unittest提供了代碼測試工具。單元測試用于核實函數的莫個方面沒有問題;測試用例是一組單元測試,這些單元測試一起核實函數在各種情形下的行為都符合要求。良好的測試用例考慮到了函數可能收到的各種輸入,包含針對所有這些情形的測試。全覆蓋測試用例包含一整套單元測試,涵蓋了各種可能的函數使用方式。對于大型項目,要實現全覆蓋可能很難,通常,最初只要針對代碼的重要行為編寫測試即可,等項目被廣泛使用時再考慮全覆蓋。

各種斷言方法

python 在unittest.TestCase 中提高了很多斷言方法。

unittest Module中的斷言方法

方法 用途 assertEqual(a,b) 核實a == b assertNotEqual(a,b) 核實a != b assertTrue(x) 核實x為True assertFalse(x) 核實x為False assertIn(item,list) 核實ietm在list中 assertNotIn(item,list) 核實item不在list中 函數測試 1.準備測試函數

name_function.py

def get_formatted_name(first, last): ’’’生成整潔的姓名’’’ full_name = first + ’ ’ + last return full_name.title()2.編寫一個能使用它的程序

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.對函數進行單元測試

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’)# 執行unittest.main()

python test_name_function.py

python中使用 unittest.TestCase單元測試的用例詳解

第一行的句點 表示測試通過了,接下來的一行指出python運行了一個測試,消耗的時間不到0.001秒,最后的OK表示改測試用例中的所有測試單元都通過了。

類測試1.準備測試的類

survey.py

class AnonmousSurvey(): '''收集匿名調查問卷的答案''' def __init__(self, question):'''存儲一個問題,并為存儲答案做準備'''self.question = questionself.responses = [] def show_question(self):'''顯示調查問卷'''print(self.question) def store_response(self, new_response):'''存儲單份調查答卷'''self.responses.append(new_response) def show_results(self):'''顯示收集到的所有答卷'''print('Survey results')for response in self.responses: print(’- ’ + response)2.編寫一個能使用它的程序

language_survey.py

from survey import AnonmousSurvey# 定義一個問題,并創建一個表示調查的AnonymousSurvey對象question = 'What language did you first learn to speak?'my_survey = AnonmousSurvey(question)# 顯示問題并存儲答案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)# 顯示調查結果print('nThank you to everyoune who participated in the survey!')my_survey.show_results()3.對類進行單元測試

import unittestfrom survey import AnonmousSurveyclass TestAnonmousSurvey(unittest.TestCase): '''針對AnonymousSurvey類的測試''' def test_store_single_response(self):'''測試單個答案會被妥善地存儲'''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):'''測試多個答案是否會被存儲'''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()

python中使用 unittest.TestCase單元測試的用例詳解

可以看到對類的單元測試也是成功的。雖然成功了,但是做法不是很好,測試有些重復了,下面使用unittest的另一項功能來提高它們的效率

方法 setUP()

如果你在TestCase類中包含方法setUP(),python將先運行它,在運行各個以test_開頭的方法。

test_survey_setup.py

import unittestfrom survey import AnonmousSurveyclass TestAnonmousSurvey(unittest.TestCase): '''針對AnonymousSurvey類的測試''' def setUp(self):'''創建一個調查對象和一組答案,供使用的測試方法使用'''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):'''測試單個答案會被妥善地存儲'''self.my_survey.store_response(self.responses[0])self.assertIn(self.responses[0], self.my_survey.responses) def test_store_three_responses(self):'''測試多個答案是否會被存儲'''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()

測試自己編寫的類時,方法setUP()讓測試方法編寫起來更容易:可以在setUP()方法中創建一系列實例并設置它們的屬性,再在測試方法中直接使用這些實例。相比于在每個測試方法中都創建實例并設置屬性,這要容易的多。

注意

運行測試用例時,每完成一個單元測試,python都打印一個字符: 測試通過時打印一個句點; 測試引發錯誤時打印一個E; 測試導致斷言失敗時打印一個F。這就是運行測試用例時,在輸出的第一行中看到的句點和字符數量各不相同的原因。如果測試用例包含很多單元測試,需要運行很長時間,就可以通過觀察這些結果來獲悉有多少個測試通過了。

到此這篇關于python中使用 unittest.TestCase 進行單元測試的文章就介紹到這了,更多相關python單元測試內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 亚洲欧洲日韩综合色天使不卡 | 亚洲国产第一区二区香蕉 | 九九久久久久久久爱 | 亚洲欧美精品久久 | 最新在线精品国自拍视频 | 日韩一区二区三区在线观看 | 国产高清在线视频 | 九九视频在线看精品 | 国产主播大尺度精品福利 | 亚洲天堂免费观看 | 天天拍拍夜夜出水 | 亚洲欧美综合网 | 热re66久久精品国产99热 | 久久五月女厕所一区二区 | 色吧久久 | 午夜爽视频 | 亚洲一区二区三区四区五区 | 国产成人深夜福利在线观看 | 色综合a | 亚洲一区二区三区免费 | 免费一级网站免费 | 日韩成人免费在线 | 好吊色综合网天天高清 | 一级片免费在线 | 午夜爽爽| 国产一级毛片午夜 | a级午夜毛片免费一区二区 a级性生活视频 | 久草中文在线视频 | 久久成人网18网站 | 成人毛片免费视频 | 网红毛片| 美女毛片免费看 | 2022年国产精品久久久久 | 欧美透逼视频 | 成人国产在线视频 | 亚洲欧美视频在线观看 | 久久无码av三级 | 看全色黄大色黄大片女图片 | 亚洲精品一区二区三区美女 | 成人黄色毛片 | 失禁h啪肉尿出来高h男男 |