# names.py from name_function import get_formatted_name print("Enter 'q' at any time to quit.") whileTrue: first = input("\nPlease give me a first name: ") if first == 'q': break last = input("Please give me a last name: ") if last == 'q': break formatted_name = get_formatted_name(first, last) print(f"\tNeatly formatted name: {formatted_name}.")
# test_survey.py import unittest from survey import AnonymousSurvey
classTestAnonymousSurvey(unittest.TestCase): """针对AnonymousSurvey类的测试""" defsetUp(self): """创建一个调查对象和一组答案,供使用的测试方法使用""" # 如果你在TestCase类包含方法setUp(),Python将先运行它,再运行各个以test_打头的方法。 question = "What language did you first learn to speak?" self.my_survey = AnonymousSurvey(question) self.responses = ['English', 'Spanish', 'Mandarin']
deftest_store_three_responses(self): """测试三个答案会被妥善地存储""" for response in self.responses: self.my_survey.store_response(response) for response in self.responses: self.assertIn(response, self.my_survey.responses)