本章知识小结:
1、单元测试(合适函数某方面没问题)、测试用例(一组单元测试)、全覆盖(一套单元测试,覆盖各种可能)
2、unittest.TestCase,如果整个文件作为被测试框架导入测试,变量__name__的值不是__main__,故不会调用unittest.main()
3、断言方法(assertEqual(a,b)、assertNotEqual(a,b)、assertTrue(x)、assertFalse(x)、assertIn(item,list)、assertNotIn(item,list))
4、setUp()方法怎么写?(导入unittest和测试类Dog,定义TestDog(unittest.TestCase)类,定义setUp方法存储即将测试的信息,定义test函数测试)
1、测试函数 单元测试 用于核实函数的某个方面没有问题。
测试用例 是一组单元测试,它们一道核实函数在各种情形下的行为都符合要求
全覆盖 的测试用例包含一整套单元测试,涵盖了各种可能得函数使用方式
1 2 3 4 5 6 7 8 9 def get_formatted_name (first, last, middle='' ): """生成整洁的姓名. 未通过测试怎么办?函数传参middle="",函数内判断middle是否存在,分开处理""" if middle: full_name = f"{first} {middle} {last} " else : full_name = f"{first} {last} " return full_name.title()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import unittestfrom name_function import get_formatted_nameclass NamesTestCase (unittest.TestCase): def test_first_last_name (self ): formatted_name = get_formatted_name('janis' , 'joplin' ) self .assertEqual(formatted_name, 'Janis Joplin' ) def test_first_last_middle_name (self ): formatted_name = get_formatted_name('wolfgang' , 'mozart' , 'amadeus' ) self .assertEqual(formatted_name, 'Wolfgang Amadeus Mozart' ) if __name__ == '__main__' : unittest.main()
2、测试类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class AnonymousSurvey : def __init__ (self, question ): self .question = question self .responses = [] def show_question (self ): print (self .question) def store_response (self, new_response ): self .responses.append(new_response) def show_results (self ): print ("调查结果:" ) for response in self .responses: print (f"- {response} " )
2.1 各种断言方法 1 2 3 4 5 6 7 8 import unitest.TestCaseassertEqual(a,b) assertNotEqual(a,b) assertTrue(x) assertFalse(x) assertIn(item,list ) assertNotIn(item,list )
2.2 测试类AnonymousSurvey 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 from survey import AnonymousSurveyquestion = "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_response(response) print ("\nThank you to everyone who participated in the survey!" )my_survey.show_results()
2.3 方法setUp() setUp()创建一系列参数并设置其属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import unittestfrom survey import AnonymousSurveyclass TestAnonymousSurvey (unittest.TestCase): def setUp (self ): 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 ): self .my_survey.store_response(self .responses[0 ]) self .assertIn(self .responses[0 ], self .my_survey.responses) def test_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) if __name__ == '__main__' : unittest.main()