第11章 测试代码

第11章 测试代码

编写函数或类时,还可为其编写测试。通过测试,可确定代码面对各种输入都能够按要求的那样工作。

1.测试函数

python提供了一种自动测试函数输出的高效方式。

单元测试和测试用例

python 标准库中的模块unittest提供了代码测试工具

单元测试用于核实函数的某个方面没有问题

测试用例是一组单元测试,它们一道核实函数在各种情形下的行为都符合要求

全覆盖的测试用例包含一整套单元测试,,涵盖了各种可能的函数使用方式

对于大型项目,要进行全覆盖测试可能很难。通常只要针对代码的重要行为编写测试即可,等项目被广泛使用时再考虑全覆盖

可通过的测试
import unittest#导入模块unittest
from name_function import get_formatted_name#导入要测试的函数

class NameTestCase(unittest.TestCase):#创建一个名为NameTestCase的类,用于包含一系列针对被测试函数的单元测试
    def test_first_last_name(self):#运行测试文件时,所有test_打头的方法都将自动运行
        formatted_name = get_formatted_name('janis','joplin')
        self.assertEqual(formatted_name,'Janis Joplin')#断言方法,断言方法核实得到的结果是否与期望的结果一致,调用unittest的方法assertEqual()
    if __name__ == '__main__':#很多测试框架都会先导入测试文件再运行,导入文件时,解释器将在导入的同时执行它
        unittest.main()
"""
Ran 1 test in 0.002s

OK
"""
未通过的测试
Ran 1 test in 0.006s

Launching unittests with arguments python -m unittest D:/Users/86180/Desktop/python编程学习笔记/源代码文件/chapter_11/myself.py in D:\Users\86180\Desktop\python编程学习笔记\源代码文件\chapter_11
FAILED (failures=1)
测试未通过时怎么办

修改被测试函数,而非测试函数

添加新测试

添加一个测试方法

import unittest

from name_function import get_formatted_name

class NamesTestCase(unittest.TestCase):
    """Tests for 'name_function.py'."""
    
    def test_first_last_name(self):
        """Do names like 'Janis Joplin' work?"""
        formatted_name = get_formatted_name('janis', 'joplin')
        self.assertEqual(formatted_name, 'Janis Joplin')

    def test_first_last_middle_name(self):
        """Do names like 'Wolfgang Amadeus Mozart' work?"""
        formatted_name = get_formatted_name(
            'wolfgang', 'mozart', 'amadeus')
        self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart')

if __name__ == '__main__':
    unittest.main()

方法名必须以test_打头,这样才会在运行测试程序时自动运行。

2.测试类

针对类的测试

各种断言方法
方法用途
assertEqual(a,b)核实a==b
assertNotEqual(a,b)核实a!=b
assertTrue(x)核实x为True
assertFalse(x)核实x为False
assertIn(item,list)核实item在list中
assertNotIn(item,list)核实item不在list中
一个要测试的类
class AnonymousSurvey:
    """Collect anonymous answers to a survey question."""
    
    def __init__(self, question):
        """Store a question, and prepare to store responses."""
        self.question = question
        self.responses = []
        
    def show_question(self):
        """Show the survey question."""
        print(self.question)
        
    def store_response(self, new_response):
        """Store a single response to the survey."""
        self.responses.append(new_response)
        
    def show_results(self):
        """Show all the responses that have been given."""
        print("Survey results:")
        for response in self.responses:
            print(f"- {response}")

from survey import AnonymousSurvey

# Define a question, and make a survey.
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)

# Show the question, and store responses to the 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)

# Show the survey results.
print("\nThank you to everyone who participated in the survey!")
my_survey.show_results()

测试AnonymousSurvey类
import unittest#导入模块unittest
from survey import  AnonymousSurvey#导入要测试的类

class TestAnonymousSurvey(unittest.TestCase):#创建一个名为NameTestCase的类,用于包含一系列针对被测试函数的单元测试
    def test_store_single_response(self):#运行测试文件时,所有test_打头的方法都将自动运行
        quesion = "what language did you first learn to speak?"
        my_survey = AnonymousSurvey(quesion)
        my_survey.store_response('English')
        self.assertIn('English',my_survey.responses)
    def test_store_three_response(self):
        quesion = "what language did you first learn to speak?"
        my_survey = AnonymousSurvey(quesion)
        responses = ['English','Spanish','Mandarin']
        for response in responses:
            my_survey.store_response(response)
        for response in responses:
            self.assertIn(response,my_survey.responses)
    if __name__ == '__main__':#很多测试框架都会先导入测试文件再运行,导入文件时,解释器将在导入的同时执行它
        unittest.main()

测试有重复的地方

方法setUp()
import unittest
from survey import AnonymousSurvey

class TestAnonymousSurvey(unittest.TestCase):
    """Tests for the class AnonymousSurvey"""
    
    def setUp(self):#使用setUp方法,python将先运行它,再运行各个以test_打头的方法
        """
        Create a survey and a set of responses for use in all test methods.
        """
        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):
        """Test that a single response is stored properly."""
        self.my_survey.store_response(self.responses[0])
        self.assertIn(self.responses[0], self.my_survey.responses)

    def test_store_three_responses(self):
        """Test that three individual responses are stored properly."""
        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()

unittest.TestCase类包含的方法setUp()让我们只需创建这些对象一次,就能在每个测试方法中使用。

可在setUpz方法中创建一系列实例并设置其属性,再在测试方法中直接使用这些实例

tips:运行测试用例时,每完成一个单元测试,python都打印一个字符:测试通过时打印一个句点,测试引发错误时打印一个E,而测试导致断言失败时则打印一个F。

如果测试用例包含很多单元测试,需要运行很长时间,就可通过观察这些结果来获悉有多少个测试通过了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱笑的刺猬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值