.
#第1行的句点表明有一个测试通过了
----------------------------------------------------------------------
Ran 1 test in 0.000s
#接下来的一行指出Python运行了一个测试,消耗的时间不到0.001秒
OK
#最后的OK表明该测试用例中的所有单元测试都通过了
#name_function.pydefget_formatted_name(first, last, middle=''):"""Generate a neatly-formatted full name."""
full_name = first +' '+ middle +' '+ last
return full_name.title()
#test_survey.pyimport unittest
from survey import AnonymousSurvey
classTestAnonymousSurvey(unittest.TestCase):"""针对AnonymousSurvey类的测试"""defsetUp(self)->None:"""创建一个问题和一组答案,供测试方法使用"""
question ="What language did you first learn to speak?"
self.my_survey = AnonymousSurvey(question)
self.responses =['English','Spanish','Mandarin']#变量名包含前缀self(即存储在属性中),因此可在这个类的任何地方使用deftest_store_single_response(self):"""测试单个答案"""
self.my_survey.store_response(self.responses[0])
self.assertIn(self.responses[0], self.my_survey.responses)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)
unittest.main()