Python Day4?
? ? 11.2 測試類
survey.py
class AnnoymousSurvey:
? ? """收集匿名調(diào)查問卷的答案"""
? ? def __init__(self,question):
? ? ? ? self.question=question
? ? ? ? self.responses=[]
? ? def show_question(self):
? ? ? ? """顯示調(diào)查問卷"""
? ? ? ? print (self.question)
? ? def store_response(self,new_response):
? ? ? ? """存儲調(diào)查問卷的答案"""
? ? ? ? self.responses.append(new_response)
? ? def show_result(self):
? ? ? ? """顯示收集到的所有答案"""
? ? ? ? print("Survey results:")
? ? ? ? for response in self.responses:
? ? ? ? ? ? print(f"- {response}")
test.survey.py
import unittest
from survey import AnnoymousSurvey
class TestAnnoymousSurvey(unittest.TestCase):
? ? """針對AnnoymousSurvey類的測試"""
? ? def test_store_single_response(self):
? ? ? ? """測試單個答案會被妥善的存儲"""
? ? ? ? question="What language did you first learn to speak?"
? ? ? ? my_survey=AnnoymousSurvey(question)
? ? ? ? my_survey.store_response('English')
? ? ? ? self.assertIn('English',my_survey.responses)
if __name__=='__main__':
? ? unittest.main()