Python單元測試unittest
阿新 • • 發佈:2018-08-09
else esp 執行 bre pri [] 運行 返回 代碼 2018年8月9日發布,後續補上註釋等文字說明
————————————
首先需要測試代碼
name_function.py是一個簡單的函數,獲取前後兩個名字,然後組合起來
def get_formatted_name(first,last,middle=‘‘): if middle: full_name = first + ‘ ‘ + middle + ‘ ‘ + last else: full_name = first + ‘ ‘ + last return full_name.title()
names.py讓用戶輸入名字,並輸出
from運行結果name_function import get_formatted_name print("enter") while True: firstname = input("enter first: ") if firstname == ‘q‘: print("exit") break lastname = input("enter second: ") if lastname == ‘q‘: print("exit") break fullname = get_formatted_name(firstname,lastname)print(‘full name: ‘,fullname)
1 enter 2 enter first: zhang 3 enter second: san 4 full name: Zhang San 5 enter first: q 6 exit
如果想要在name_function.py函數的first和last添加一個中間名,為了驗證之前的函數是否能夠正常工作,可以在每次修改後都去驗證get_formatted_name(first,last)進行測試,未免太麻煩了。 Python提供了這樣一種自動測試函數輸出的方法,unittest。
import unittest以上代碼,導入模塊以及要被測試的函數, 通過調用函數,斷言與返回的formatted_name判斷結果,最後unittest.main()執行以上結果。 運行結果from name_function import get_formatted_name class NameTestCase(unittest.TestCase): def test_first_last_name(self): formatted_name = get_formatted_name(‘janis‘,‘joplin‘) self.assertEqual(formatted_name,‘Janis Joplin‘) def test_first_last_middle_name(self): formatted_name = get_formatted_name(‘wolfgang‘,‘mozart‘,‘amadeus‘) self.assertEqual(formatted_name,‘Wolfgang Amadeus Mozart‘) unittest.main()
.. ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK
再寫一段代碼 survey.py
class AnoymousSurvey(): def __init__(self,question): self.question = question self.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)
language_servey.py
from survey import AnoymousSurvey question = "what language you first learn" my_survey = AnoymousSurvey(question) my_survey.show_question() print("enter q to quit") while True: response = input("language: ") if response == ‘q‘: break my_survey.store_response(response) print("--") print("thank your input") my_survey.show_results()
通過用戶輸入信息,然後存入列表,接著打印出來 接著使用unittest,test_survey.py
from survey import AnoymousSurvey import unittest class TestAnonymousSurvey(unittest.TestCase): def test_store_single_response(self): question = "what language you first learn1" my_survey = AnoymousSurvey(question) my_survey.show_question() my_survey.store_response(‘English‘) self.assertIn(‘English‘,my_survey.responses) def test_store_three_response(self): question = "what language you first learn2" my_survey = AnoymousSurvey(question) my_survey.show_question() responses = [‘English‘,‘Spanish‘,‘Chinese‘] for response in responses: my_survey.store_response(response) self.assertIn(response,my_survey.responses) if __name__ == ‘__main__‘: unittest.main()
或者使用setUp方法
import unittest from survey import AnoymousSurvey class TestAnonymousSurvey(unittest.TestCase): def SetUp(self): question = "what language you first learn" self.my_survey = AnoymousSurvey(question) self.responses = [‘English‘,‘Spanish‘,‘Chinese‘] 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_response(self): for response in self.responses: self.my_survey.store_response(response) for response in self.responses: self.assertIn(response,self.my_survey.response) if __name__ == ‘__main__‘: unittest.main()
Python單元測試unittest