Python單元測試unittest

來源:互聯網
上載者:User

標籤:一個   ini   last   zhang   __init__   code   unittest   lse   format   

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 enter2 enter first: zhang3 enter second: san4 full name:  Zhang San5 enter first: q6 exit

 

如果想要在name_function.py函數的first和last添加一個中間名,為了驗證之前的函數是否能夠正常工作,可以在每次修改後都去驗證get_formatted_name(first,last)進行測試,未免太麻煩了。Python提供了這樣一種自動化的測試函數輸出的方法,unittest。
import unittestfrom 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()
以上代碼,匯入模組以及要被測試的函數,通過調用函數,斷言與返回的formatted_name判斷結果,最後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 AnoymousSurveyimport 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 unittestfrom 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.