部分**來自《python程式設計從入門到實踐》一書
執行測試用例時,每完成乙個單元測試,python都列印乙個字元:測試通過列印乙個句點,錯誤列印 e,測試導致斷言失敗列印乙個 f
針對單個函式的測試:
#name_function.py中
defget_formatted_name
(first,last,middle=''):
if middle:
full_name = first + ' ' + middle + ' ' + last
else:
full_name = first + ' ' + last
return full_name.title()
----------
#測試模組:test_name_function.py
import unittest
from name_function import get_formatted_name
class
nametestcase
(unittest.testcase):
"""測試name_function.py"""
deftest_first_last_name
(self):
"""能夠正確地處理像janis joplin樣的姓名嗎"""
formatted_name = get_formatted_name('janis','joplin')
self.assertequal(formatted_name,'janis joplin')
deftest_first_last_middle_name
(self):
"""能夠正確處理三個串的名字嗎"""
formatted_name = get_formatted_name('wolgang','mozart','amadeus')
self.assertequal(formatted_name,'wolgang amadeus mozart')
unittest.main()
測試類#要測試的類在suevey.py中
class
anonymoussurvey
():"""收集匿名問卷的答案"""
def__init__
(self,question):
"""儲存乙個問題,並未儲存答案做準備"""
self.question = question
self.responses =
defshow_question
(self):
"""顯示調查問卷"""
print(self.question)
defstore_response
(self,new_response):
"""儲存單份調查問卷"""
defshow_results
(self):
"""顯示收集到的所有問卷"""
print("survery results:")
for response in self.responses:
print('- ' + response)
----------
#測試的方法在test_suevey.py中
import unittest
from survey import anonymoussurvey
class
testanonymoussurvey
(unittest.testcase):
"""針對anony...的測試"""
deftest_store_single_response
(self):
#用於測試的方法以test_開頭,自動執行
"""測試單個答案會被妥善儲存"""
question = "what language did you first learn to speak?"
my_survey = anonymoussurvey(question) #建立例項
my_survey.store_response('english') #儲存答案
self.assertin('english',my_survey.responses) #斷言,指出要測試的是responses列表中是否包含'english'
deftest_store_three_responses
(self):
"""測試三個答案會被妥善儲存"""
question = "what language did you first learn to speak?"
my_survey = anonymoussurvey(question)
responses = ['english','chinese','mandarin']
for response in responses:
my_survey.store_response(response)
for response in responses:
self.assertin(response,my_survey.responses)
unittest.main()
import unittest
from survey import anonymoussurvey
class
testanonymoussurvey
(unittest.testcase):
"""針對anony...的測試"""
defsetup
(self):
"""建立乙個調查物件和一組答案,供使用的測試方法使用"""
question = "what language did you first learn to speak?"
self.my_survey = anonymoussurvey(question)
self.responses = ['english','chinese','mandarin']
deftest_store_single_response
(self):
#用於測試的方法以test_開頭,自動執行
"""測試單個答案會被妥善儲存"""
self.my_survey.store_response(self.responses[0]) #儲存答案
self.assertin(self.responses[0],self.my_survey.responses) #斷言,指出要測試的是responses列表中是否包含'english'
deftest_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()
Python學習筆記 測試
測試分類 單元測試 全覆蓋測試 單元測試 測試方法 引入unittest 例 name function.py def get formatted name first,middle,last full name first middle last return full name.title te...
介面測試基礎(學習筆記)
一 介面測試分類 get post delete put 等等 二 為什麼要做介面測試 三 post和get的區別 get請求最大url長度是2048字元,post無限制。get可以被收藏為書籤,post不行。get能被快取,post不行。二者編碼型別不同,post多了二進位制資料多重編碼。get的...
python學習筆記 Python基礎
雲計算web開發 django框架 科學計算 人工智慧 常用到的庫有numpy pandas matplotlib 等等 系統運維 金融 圖形gui google 豆瓣 知乎 facebook 主要特點是 解釋性 動態語言 強型別定義語言和弱型別定義語言。解釋型 python擁有良好的相容性,在安裝...