#定义一个问题,并创建一个表示调查的 AnonymousSurvey 对象question = "what language did you first learn to speak?"my_srrvey = AnonymousSurvey(question)my_srrvey.show_question()print("Enter 'q' at any time to quit\n")while True: response = input("Language:") if response == 'q': break my_srrvey.store_response(response)#显示调查结果print("\nThank you to everyone who participated in survey!")my_srrvey.show_results()
复制代码
下面我们来编写一个测试类。
测试类
复制代码
[/code]
[code]import unittest
复制代码
from survey import AnonymousSurveyclass TestSurvey(unittest.TestCase): """针对AnonymousSurvey类的测试""" def test_store_single_response(self): """测试单个答案会被妥善的保存""" question = "what language did you first learn to speak?" my_srrvey = AnonymousSurvey(question) my_srrvey.store_response("Chinese") self.assertIn('Chinese',my_srrvey.responses)unittest.main()
import unittestfrom survey import AnonymousSurveyclass TestSurvey(unittest.TestCase): """针对AnonymousSurvey类的测试""" def setUp(self): """创建一个调查对象和一组答案 共使用的测试方法使用""" question = "what language did you first learn to speak?" self.my_survey = AnonymousSurvey(question) self.responses = ["Chinese",'English','Spanish'] 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)unittest.main()