1.各種斷言方法 常用斷言方法: 方法 用途 assertEqual(a, b) 核實a == b assertNotEqual(a, b) 核實a != b assertTrue(x) 核實x為True assertFalse(x) 核實x為False asseertIn(item, list) ...
1.各種斷言方法
常用斷言方法:
方法 |
用途 |
assertEqual(a, b) |
核實a == b |
assertNotEqual(a, b) |
核實a != b |
assertTrue(x) |
核實x為True |
assertFalse(x) |
核實x為False |
asseertIn(item, list) |
核實item在list中 |
assertNotIn(item, list) |
核實item不在list中 |
2.一個要測試的類
首先編寫一個類:
survey.py
class AnonymousSurvey: """收集匿名調查問卷的答案""" def __init__(self, question): """存儲一個問題,併為存儲答案做準備""" self.question = question self.responses = [] def show_quetion(self): """顯示調查問卷""" print(self.question) def store_response(self, new_response): """存儲單份調查答案""" self.responses.append(new_response) def show_result(self): """顯示收集到的所有答卷""" print("Survey result:") for response in self.responses: print('- ' + response)
為證明AnonymousSurvey類可以正確的工作,編寫一個使用它的程式:
language_survey.py
from survey import AnonymousSurvey # 定義一個問題,並創建一個表示調查的AnonymousSurvey對象 question = "What language did you first learn to speak?" my_survey = AnonymousSurvey(question) # 顯示並存儲問題的答案 my_survey.show_quetion() 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 everyone who participated in survey!") my_survey.show_result()
運行結果:
What language did you first learn to speak? Enter 'q' at any time to quit. Language: English Language: Spanish Language: English Language: Mandarin Language: q Thank you to everyone who participated in survey! Survey result: - English - Spanish - English - Mandarin
3.測試AnonymousSurvey類
對AnonymousSurvey類行為的一個方面進行驗證:如果用戶面對調查問題時只提供了一個答案,這個答案也能被妥善地存儲。使用方法assertIn()來核實它包含在答案列表中:
test_survey.py
import unittest from survey import AnonymousSurvey class TestAnonymousSurvey(unittest.TestCase): """針對AnonymousSurvey類的測試""" def test_store_single_response(self): """測試單個答案會被妥善地存儲""" question = "What language did you first learn to speak?" my_survey = AnonymousSurvey(question) my_survey.store_response('English') self.assertIn('English', my_survey.responses) if __name__ == "__main__": unittest.main()
運行test_survey.py時,測試通過了:
Ran 1 test in 0.010s OK
只能收集一個答案的調查用途不大。下麵核實用戶提供三個答案時,它們也將被妥善地存儲。為此,在AnonymousSurvey中再添加一個方法:
import unittest from survey import AnonymousSurvey class TestAnonymousSurvey(unittest.TestCase): """針對AnonymousSurvey類的測試""" def test_store_single_response(self): ... def test_store_three_response(self): """測試三個答案會被妥善地存儲""" question = "What language did you first learn to speak?" my_survey = AnonymousSurvey(question) responses = ['English', 'Spanish', 'Mandarin'] for response in responses: my_survey.store_response(response) for response in responses: self.assertIn(response, my_survey.responses) if __name__ == "__main__": unittest.main()
再次運行test_survey.py時,兩個測試都通過了:
Ran 2 tests in 0.002s OK
這些還有些重覆的地方,怎麼更簡潔呢?
4.方法setUp()
unittest.TestCase類包含方法setUp(),只需創建這些對象一次,併在每個測試方法中使用它們。如果在TestCase類中包含了方法setUp(),Python將先運行它,再運行各個以test_打頭的方法。這樣,在編寫的每個測試方法中都可使用方法setUp()中創建的對象。
使用setUp()來創建一個調查對象和一組答案,供方法test_store_single_response()和test_store_three_responses()使用:
import unittest from survey import AnonymousSurvey class TestAnonymousSurvey(unittest.TestCase): """針對AnonymousSurvey類的測試""" 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_response(self): """測試三個答案會被妥善地存儲""" for response in self.responses: self.my_survey.store_response(response) for response in self.responses: self.assertIn(response, self.my_survey.responses) if __name__ == "__main__": unittest.main()
方法setUp()做了兩件事:創建一個調查對象;創建一個答案列表。存儲這兩樣東西的變數名包含首碼self,因此可以在這個類中的任何地方使用。這個讓兩個測試方法都更加簡單,不用創建調查對象和答案。
測試自己編寫的類時,方法setUp()讓測試方法編寫起來更容易:可在setUp中創建一系列實例並設置它們的屬性,再在測試方法中直接使用這些實例。相比於每個測試方法中都創建實例並設置其屬性要容易得多。