一、Python测试基础概述
在Python编程中,测试是至关重要的环节。它能确保代码在各种可能的输入情况下都能正确运行。当我们在项目中添加新的代码时,测试可以帮助我们快速发现新代码是否对原有功能造成了破坏。通过系统的测试,我们可以提高代码的可靠性和稳定性,减少程序运行时出现错误的概率。
二、各种断言方法详解
(一)常用断言方法介绍
import unittestclassTestStringMethods(unittest.TestCase):deftest_assertEqual(self): a = 5 b = 5 self.assertEqual(a, b)
import unittestclassTestStringMethods(unittest.TestCase):deftest_assertNotEqual(self): a = 5 b = 3 self.assertNotEqual(a, b)
import unittestclassTestStringMethods(unittest.TestCase):deftest_assertTrue(self): a = True self.assertTrue(a)
import unittestclassTestStringMethods(unittest.TestCase):deftestassertFalse(self): a = False self.assertFalse(a)
- 用途:判断一个元素是否在一个容器(如列表、字典等)中。
import unittestclassTestStringMethods(unittest.TestCase):deftest_assertIn(self): my_list = [1, 2, 3] a = 2 self.assertIn(a, my_list)
import unittestclassTestStringMethods(unittest.TestCase):deftest_assertNotIn(self): my_list = [1, 2, 3] a = 4 self.assertNotIn(a, my_list)
import unittestclassTestStringMethods(unittest.TestCase):deftest_assertIsNone(self): a = None self.assertIsNone(a)
import unittestclassTestStringMethods(unittest.TestCase):deftest_assertIsNotNone(self): a = 5 self.assertIsNotNone(a)
三、一个要测试的类
(一)类的定义与功能
以AnonymousSurvey类为例,它用于收集匿名调查问卷的答案。
classAnonymousSurvey:def__init__(self, question): self.question = question self.responses = []defshow_question(self): print(self.question)defstore_response(self, response): self.responses.append(response)defshow_results(self): print("Survey results:")for response in self.responses: print('-'+ response)
(二)类的使用示例
以下是使用这个类的示例:
question = "What language did you first learn to program in?"my_survey = AnonymousSurvey(question)my_survey.show_question()response1 = "Python"my_survey.store_response(response1)my_survey.show_results()
四、测试AnonymousSurvey类
(一)单个答案存储测试
import unittestclassTestAnonymousSurvey(unittest.TestCase):deftest_store_single_response(self): question = "What is your favorite color?" my_survey = AnonymousSurvey(question) response = "Blue" my_survey.store_response(response) self.assertIn(response, my_survey.responses)
(二)多个答案存储测试
import unittestclassTestAnonymousSurvey(unittest.TestCase):deftest_store_multiple_responses(self): question = "What is your favorite fruit?" my_survey = AnonymousSurvey(question) responses = ["Apple", "Banana", "Orange"]for response in responses: my_survey.store_response(response)for response in responses: self.assertIn(response, my_survey.responses)
五、方法setUp()的优势
(一)简化测试过程
在测试类时,方法setUp()非常有用。它可以在每个测试方法运行之前创建一些必要的对象和数据。例如,在测试AnonymousSurvey类时,如果我们没有使用setUp()方法,我们可能需要在每个测试方法中重复创建AnonymousSurvey对象和相关数据。
import unittestclassTestAnonymousSurveyWithoutSetUp(unittest.TestCase):deftest_store_single_response_without_setUp(self): question = "What is your favorite animal?" my_survey = AnonymousSurvey(question) response = "Dog" my_survey.store_response(response) self.assertIn(response, my_survey.responses)deftest_store_multiple_responses_without_setUp(self): question = "What is your favorite flower?" my_survey = AnonymousSurvey(question) responses = ["Rose", "Lily", "Tulip"]for response in responses: my_survey.store_response(response)for response in responses: self.assertIn(response, my_survey.responses)
(二)提高测试效率
使用setUp()方法可以提高测试效率。我们来看使用setUp()的测试示例:
import unittestclassTestAnonymousSurveyWithSetUp(unittest.TestCase):defsetUp(self): self.question = "What is your favorite subject?" self.my_survey = AnonymousSurvey(self.question) self.responses = ["Math", "Science", "History"]deftest_store_single_response_with_setUp(self): response = self.responses[0] self.my_survey.store_response(response) self.assertIn(response, self.my_survey.responses)deftest_store_multiple_responses_with_setUp(self):for response in self.responses: self.my_survey.store_response(response)for response in self.responses: self.assertIn(response, self.my_survey.responses)
通过setUp()方法,我们在测试类中只需要定义一次对象和数据,多个测试方法都可以直接使用,减少了重复代码,提高了测试效率。