python 入门到实践期末考试常出现的考试内容_测试类——python编程从入门到实践...
1.各種斷言方法
常用斷言方法:
方法
用途
assertEqual(a, b)
核實(shí)a == b
assertNotEqual(a, b)
核實(shí)a != b
assertTrue(x)
核實(shí)x為T(mén)rue
assertFalse(x)
核實(shí)x為False
asseertIn(item, list)
核實(shí)item在list中
assertNotIn(item, list)
核實(shí)item不在list中
2.一個(gè)要測(cè)試的類
首先編寫(xiě)一個(gè)類:
survey.py
classAnonymousSurvey:"""收集匿名調(diào)查問(wèn)卷的答案"""
def __init__(self, question):"""存儲(chǔ)一個(gè)問(wèn)題,并為存儲(chǔ)答案做準(zhǔn)備"""self.question=question
self.responses=[]defshow_quetion(self):"""顯示調(diào)查問(wèn)卷"""
print(self.question)defstore_response(self, new_response):"""存儲(chǔ)單份調(diào)查答案"""self.responses.append(new_response)defshow_result(self):"""顯示收集到的所有答卷"""
print("Survey result:")for response inself.responses:print('-' + response)
為證明AnonymousSurvey類可以正確的工作,編寫(xiě)一個(gè)使用它的程序:
language_survey.py
from survey importAnonymousSurvey#定義一個(gè)問(wèn)題,并創(chuàng)建一個(gè)表示調(diào)查的AnonymousSurvey對(duì)象
question = "What language did you first learn to speak?"my_survey=AnonymousSurvey(question)#顯示并存儲(chǔ)問(wèn)題的答案
my_survey.show_quetion()print("Enter 'q' at any time to quit.\n")whileTrue:
response= input("Language:")if response == 'q':breakmy_survey.store_response(response)#顯示調(diào)查結(jié)果
print("\nThank you to everyone who participated in survey!")
my_survey.show_result()
運(yùn)行結(jié)果:
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 participatedinsurvey!
Survey result:-English-Spanish-English- Mandarin
3.測(cè)試AnonymousSurvey類
對(duì)AnonymousSurvey類行為的一個(gè)方面進(jìn)行驗(yàn)證:如果用戶面對(duì)調(diào)查問(wèn)題時(shí)只提供了一個(gè)答案,這個(gè)答案也能被妥善地存儲(chǔ)。使用方法assertIn()來(lái)核實(shí)它包含在答案列表中:
test_survey.py
importunittestfrom survey importAnonymousSurveyclassTestAnonymousSurvey(unittest.TestCase):"""針對(duì)AnonymousSurvey類的測(cè)試"""
deftest_store_single_response(self):"""測(cè)試單個(gè)答案會(huì)被妥善地存儲(chǔ)"""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()
運(yùn)行test_survey.py時(shí),測(cè)試通過(guò)了:
Ran 1 test in0.010s
OK
只能收集一個(gè)答案的調(diào)查用途不大。下面核實(shí)用戶提供三個(gè)答案時(shí),它們也將被妥善地存儲(chǔ)。為此,在AnonymousSurvey中再添加一個(gè)方法:
importunittestfrom survey importAnonymousSurveyclassTestAnonymousSurvey(unittest.TestCase):"""針對(duì)AnonymousSurvey類的測(cè)試"""
deftest_store_single_response(self):
...deftest_store_three_response(self):"""測(cè)試三個(gè)答案會(huì)被妥善地存儲(chǔ)"""question= "What language did you first learn to speak?"my_survey=AnonymousSurvey(question)
responses= ['English', 'Spanish', 'Mandarin']for response inresponses:
my_survey.store_response(response)for response inresponses:
self.assertIn(response, my_survey.responses)if __name__ == "__main__":
unittest.main()
再次運(yùn)行test_survey.py時(shí),兩個(gè)測(cè)試都通過(guò)了:
Ran 2 tests in0.002s
OK
這些還有些重復(fù)的地方,怎么更簡(jiǎn)潔呢?
4.方法setUp()
unittest.TestCase類包含方法setUp(),只需創(chuàng)建這些對(duì)象一次,并在每個(gè)測(cè)試方法中使用它們。如果在TestCase類中包含了方法setUp(),Python將先運(yùn)行它,再運(yùn)行各個(gè)以test_打頭的方法。這樣,在編寫(xiě)的每個(gè)測(cè)試方法中都可使用方法setUp()中創(chuàng)建的對(duì)象。
使用setUp()來(lái)創(chuàng)建一個(gè)調(diào)查對(duì)象和一組答案,供方法test_store_single_response()和test_store_three_responses()使用:
importunittestfrom survey importAnonymousSurveyclassTestAnonymousSurvey(unittest.TestCase):"""針對(duì)AnonymousSurvey類的測(cè)試"""
defsetUp(self):"""創(chuàng)建一個(gè)調(diào)查對(duì)象和一組答案,供使用的測(cè)試方法使用"""question= "What language did you first learn to speak?"self.my_survey=AnonymousSurvey(question)
self.responses= ['English', 'Spanish', 'Mandarin']deftest_store_single_response(self):"""測(cè)試單個(gè)答案會(huì)被妥善地存儲(chǔ)"""self.my_survey.store_response(self.responses[0])
self.assertIn(self.responses[0], self.my_survey.responses)deftest_store_three_response(self):"""測(cè)試三個(gè)答案會(huì)被妥善地存儲(chǔ)"""
for response inself.responses:
self.my_survey.store_response(response)for response inself.responses:
self.assertIn(response, self.my_survey.responses)if __name__ == "__main__":
unittest.main()
方法setUp()做了兩件事:創(chuàng)建一個(gè)調(diào)查對(duì)象;創(chuàng)建一個(gè)答案列表。存儲(chǔ)這兩樣?xùn)|西的變量名包含前綴self,因此可以在這個(gè)類中的任何地方使用。這個(gè)讓兩個(gè)測(cè)試方法都更加簡(jiǎn)單,不用創(chuàng)建調(diào)查對(duì)象和答案。
測(cè)試自己編寫(xiě)的類時(shí),方法setUp()讓測(cè)試方法編寫(xiě)起來(lái)更容易:可在setUp中創(chuàng)建一系列實(shí)例并設(shè)置它們的屬性,再在測(cè)試方法中直接使用這些實(shí)例。相比于每個(gè)測(cè)試方法中都創(chuàng)建實(shí)例并設(shè)置其屬性要容易得多。
總結(jié)
以上是生活随笔為你收集整理的python 入门到实践期末考试常出现的考试内容_测试类——python编程从入门到实践...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 兔子、狼、老虎的故事
- 下一篇: python批量更改word文件名_py