python 測試代碼

來源:互聯網
上載者:User

標籤:with   res   測試載入器   you   編寫   變數   失敗   color   完成   

編寫函數或者類時進行測試,確保代碼正常工作

python模組unittest 提供了代碼測試載入器。

單元測試用於核實函數的某個方面沒有問題;

測試案例是一組單元測試,這些單元測試一起核實函數在各種情況選的行為都符合要求

# Author:songimport unittestclass TestString(unittest.TestCase):    def test_upper(self):        self.assertEqual(‘Foo‘.upper(),‘FOO‘)    def test_isupper(self):        self.assertTrue(‘FOO‘.isupper())        self.assertFalse(‘Foo‘.isupper())    def test_split(self):        s= ‘hello world‘        self.assertEqual(s.split(),[‘hello‘,‘world‘])        with self.assertRaises(TypeError):            s.split(2)if __name__=="__main__":    unittest.main()

  unittest.main():使用它可以將一個單元測試模組變為可直接啟動並執行測試指令碼,main()方法使用TestLoader類來搜尋所有包含在該模組中以“test”命名開頭的測試方法,並自動執行

 

  Python在unittest.TestCase類中提供了很多斷言方法

常見幾個

 

  unittest.TestCase類包含方法setUp(),讓我們只需建立這些對象一 次,並在每個測試方法中使用它們。

AnonymousSurvey.py

class AnonymousSurvey():#建立匿名調查的類    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 result:‘)        for response in self.responses:            print(‘-‘+response)

測試代碼,

import unittestclass TestAnonymousSurvey(unittest.TestCase):    def setUp(self):        question = "What language did you first learn to speak?"        self.my_survey = AnonymousSurvey(question)        self.responses = [‘English‘, ‘Spanish‘, ‘Mandarin‘]    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()做了兩件事情:建立一個調查對象;建立一個答案列表。儲存這兩樣東西的變數名包含首碼self(即儲存在屬性中),因此可在這個類的任何地方使用。這讓兩個測試方法都更簡單,因為它們都不用建立調查對象和答案。方法setUp()讓測試方法編寫起來更容易,相比於在每個測試方法中都創 建執行個體並設定其屬性,這要容易得多。

運行結果

..----------------------------------------------------------------------Ran 2 tests in 0.000sOK

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

 

python 測試代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.