class TestAnonymousSurvey(unittest.TestCase): def setUp(self): """ 事先创建一个调查对象和一组答案,供使用的测试方法使用 相当于__init__ """ 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): # question = "What language did you first learn to speak?" # my_survey = AnonymousSurvey(question) # my_survey.store_question('English') self.my_survey.store_question(self.responses[0]) self.assertIn('English', self.my_survey.responses) def test_store_three_responses(self): # question = "What language did you first learn to speak?" # my_survey = AnonymousSurvey(question) # responses = ['English', 'Spanish', 'Mandarin'] for response in self.responses: self.my_survey.store_question(response) for response in self.responses: # 检查一个response是否在Survey中的responses的列表里面 self.assertIn(response, self.my_survey.responses)
class TestAnonymousSurvey(unittest.TestCase): """Tests the class""" def setUp(self): """Create a survey and set of responses to use in all test cases""" question = "What was your first langauge? " self.my_survey = AnonymousSurvey(question) self.my_responses = ['English', 'Spanish', 'Malayalam'] def test_store_single_response(self): """Test to see if a single response is stored correctly""" self.my_survey.store_question(self.my_responses[0]) self.assertIn(self.my_responses[0], self.my_survey.responses) def test_store_three_responses(self): """Test to see if three responses is stored correctly""" for response in self.my_responses: self.my_survey.store_question(response) for response in self.my_responses: self.assertIn(response, self.my_survey.responses)
from survey import AnonymousSurvey #Define a question and make a survey question = "What language did you first learn to speak?" language_survey_instance = AnonymousSurvey(question) #Show question and store responses to the question language_survey_instance.show_question() print("Press q at any time to quit: ") while True: specific_response = input("What was your first lanaguage? ") if specific_response == 'q': break language_survey_instance.store_question(specific_response) #Show the survey results print("\nThank you for taking the survey. Please see the results: ") language_survey_instance.print_responses()
from survey import AnonymousSurvey question = "What language did you first learn to speak?" my_survey = AnonymousSurvey(question) my_survey.show_question() print("Enter 'q' at any time to quit.\n") while True: response = input("Language: ") if response == 'q': break my_survey.store_question(response) # 显示调查结果 print("\nThank you to everyone who participated in the survey!") my_survey.show_results()