Python 第八章 测试代码


前言

    编写函数或类时,还可以为其编写测试,通过测试,可以确定代码面对各种输入都能够按要求那样工作。
    通常我们使用 python 模块 unittest 中的工具来测试代码。


一、测试函数

1.1 单元测试和测试用例

  • 单元测试:用于核实函数的某个方面没有问题
  • 测试用例:是一组单元测试,这些单元测试一起核实函数在各种情形下的行为都符合要求。
  • 全覆盖式测试用例:包含一整套单元测试,涵盖了各种可能的函数使用方式。

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

1.2 可通过的测试

    要为函数编写测试用例,可先导入模块 unittest 以及要测试的函数,在创建一个继承 unittest.TestCase 的类,并编写一系列方法对函数行为的不同方面进行测试。

代码如下(示例):


Lianxie.py 中需要编写测试的函数
def get_formatted_name(first, last):
    """生成整洁的名字"""
    full_name = first + ' ' + last
    return full_name.title()

lianxi2.py 编写测试用例
import unittest
# 导入Lianxi.py中需要编写测试用例的函数 get_formatted_name
from Lianxi import get_formatted_name  


class NameTestCase(unittest.TestCase):
    """测试Lianxi中的getforttedname函数"""
    def test_first_last_name(self):
        """能正确处理像Janis Joplin这样的姓名吗"""
        formatted_name = get_formatted_name('janis', 'joplin')
        self.assertEqual(formatted_name, 'Janis Joplin')

unittest.main()

输出:
----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

进程已结束,退出代码 0

    上述我们在测试用例中创建了一个名为NameTestCase的类,用于包含一系列针对 get_formatted_name() 函数的单元测试,这个类必须继承 unittest.TestCase 类,这样才知道如何运行你编写的测试。

    我们在NameTestCase类中命名方法时必须以 test_ 打头,因为当运行lianxi.py时所有以 test_ 打头的方法都将自动运行。

    self.assertEqual(a,b) 是一个断言方法,它是unittest类最有用的功能之一,用于核实 a==b,如果不相等则测试用例不通过。

1.3 不能通过的测试

    我们故意修改Lianxi.py中的get_formatted_name() 函数,让它不能正确处理像Janise Joplin 这样的姓名。
代码如下(示例):


Lianxie.py 中需要编写测试的函数
def get_formatted_name(first,  middle, last):
    """生成整洁的名字"""
    full_name = first + ' ' + middle + ' ' + last
    return full_name.title()

lianxi2.py 编写测试用例
import unittest
# 导入Lianxi.py中需要编写测试用例的函数 get_formatted_name
from Lianxi import get_formatted_name  


class NameTestCase(unittest.TestCase):
    """测试Lianxi中的getforttedname函数"""
    def test_first_last_name(self):
        """能正确处理像Janis Joplin这样的姓名吗"""
        formatted_name = get_formatted_name('janis', 'joplin')
        self.assertEqual(formatted_name, 'Janis Joplin')

unittest.main()

输出:
----------------------------------------------------------------------

Traceback (most recent call last):
  File "D:/PythonWorkSpace/lianxi2.py", line 80, in test_first_last_name
    formatted_name = get_formatted_name('janis', 'joplin')
TypeError: get_formatted_name() missing 1 required positional argument: 'last'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

进程已结束,退出代码 1

    此时测试不能通过,Traceback 中的TypeError 提示 get_formatted_name() 出错,缺少一个必不可少的位置参数。

1.4 测试未通过时怎么办

    测试未通过时,不要急于修改代码,而是应找出错误位置,根据错误提示找出导致函数行为不符合预期的原因,再修改代码。

    上述中的错误原因是在 get_formatted_name() 函数中添加了一个middle参数,它表示中间名。它可分为有中间名和没有中间名两种情况,所以我们可以给middle形参赋初值为空,有中间名则传入实参替代原始的空值,没有中间名则使用空值。

代码如下(示例):


Lianxie.py 中需要编写测试的函数
def get_formatted_name(first, last, middle=''):
    """生成整洁的名字"""
    if middle:
        full_name = first + ' ' + middle + ' ' + last
    else:
        full_name = first + ' ' + last
    return full_name.title()

lianxi2.py 编写测试用例
class NameTestCase(unittest.TestCase):
    """测试Lianxi中的getforttedname函数"""
    def test_first_last_name(self):
        """能正确处理像Janis Joplin这样的姓名吗"""
        formatted_name = get_formatted_name('janis', 'joplin')
        self.assertEqual(formatted_name, 'Janis Joplin')

    def test_first_middle_last_name(self):
        """能正确处理像Wolfgang Amadeus Mozart这样的姓名"""
        formatted_name = get_formatted_name('wolfgang', 'mozart', 'amadeus')
        self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart')


unittest.main()

输出:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

进程已结束,退出代码 0

    这样我们就解决了测试未通过的问题。

二、测试类

    之前我们编写了针对函数的测试,下面将编写针对类的测试,因为程序中都会用到类,如果类的测试通过了,你就能确信对类所做的改进没有意外的破坏其原有行为。

2.1 各种断言方法

    Python 在 unittest.TestCase 类中提供了很多断言方法,如下表所示:

方法用途
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 中

2.2 一个要测试的类

    类的测试与函数的测试相似——你所做的大部分工作都是测试类中方法的行为,但也存在一些不同之处。
代码如下(示例):Lianxi.py中的一个帮助管理匿名调查的类

class AnonymousSurvey(object):
    """收集匿名调查问卷"""
    def __init__(self, question):
        """存储一个问题,并为存储答案做准备"""
        self.question = question
        self.responses = []

    def show_question(self):
        """显示调查问卷"""
        print(self.responses)

    def story_response(self, new_responses):
        """存储单份调查答案"""
        self.responses.append(new_responses)

    def show_results(self):
        """显示所有收集到的答案"""
        print("Survey results:")
        for response in self.responses:
            print('- ' + response)

2.3 测试AnonumousSurvey类

    编写一个测试,对AnonumousSurvey类行为的一个方面进行验证:如果用户只提供一个答案,这个答案也能被妥善存储。对此,我们在这个答案被存储后,使用assertIn() 来核实它包含在答案列表中。


lianxi2.py 编写测试用例
from Lianxi import AnonymousSurvey


class TestAnonymousSurvey(unittest.TestCase):
    """针对AnonymousSurvey类的测试"""
    def test_store_single_response(self):
        """测试单个答案会被妥善的存储"""
        question = "What language did you first learn to speak?"
        # 创建名为 my_suevey 的实例
        my_survey = AnonymousSurvey(question)
        # 使用story_response() 方法存储单个答案 English
        my_survey.story_response('English')
        # 使用assertIn() 来核实单个答案包含在答案列表中
        self.assertIn('English', my_survey.responses)


unittest.main()

输出:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

进程已结束,退出代码 0

    测试通过了,但只能收集一个答案的调查用途不大,下面来核实用户提供三个答案时,它们也能被妥善地存储。为此,我们在TestAnonymousSurvey 中再添加一个方法。


lianxi2.py 编写测试用例
from Lianxi import AnonymousSurvey


class TestAnonymousSurvey(unittest.TestCase):
    """针对AnonymousSurvey类的测试"""
    def test_store_single_response(self):
        """测试单个答案会被妥善的存储"""
        question = "What language did you first learn to speak?"
        # 创建名为 my_survey 的实例
        my_survey = AnonymousSurvey(question)
        # 使用story_response() 方法存储单个答案 English
        my_survey.story_response('English')
        # 使用assertIn() 来核实单个答案包含在答案列表中
        self.assertIn('English', my_survey.responses)

    def test_story_three_responses(self):
        """测试三个答案会被妥善存储"""
        question = "What language did you first learn to speak?"
        # 创建名为 my_survey 的实例
        my_survey = AnonymousSurvey(question)
        # 创建包含三个答案的列表
        responses = ['English', 'Spanish', 'Mandarin']
        # 使用story_response() 方法存储responses中的三个答案
        for response in responses:
            my_survey.story_response(response)
        # 使用assertIn() 来核实三个答案包含在答案列表中
        for response in responses:
            self.assertIn(response, my_survey.responses)    


unittest.main()

输出:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

进程已结束,退出代码 0

    上述的测试结果都是正确的,但是两个测试方法中都有重复代码,下面将使用 unittest 的另一项功能来提高它们的效率。

2.4 方法setup()

    上述的每个测试方法中,我们都创建了一个AnonymousSurvey实例,并在每个方法中都创建了答案。unittest.TestCase 类包含方法 setup(),让我们只需创建这些对象一次,并在每个测试方法中使用它们,python 会先运行 TestCase 类中的 setup() 方法,然后再运行各个以 test_ 打头的方法。

    下面使用 setup() 创建一个调查对象和一组答案,供方法 test_store_single_response() 和 test_store_three_responses() 使用:


lianxi2.py 编写测试用例
from Lianxi import AnonymousSurvey


class TestAnonymousSurvey(unittest.TestCase):
    """针对AnonymousSurvey类的测试"""
    def setUp(self):
        """
        创建一个调查对象和一组答案,供使用的测试方法使用
        """
        question = "What language did you first learn to speak?"
        # 创建名为 my_survey 的实例
        self.my_survey = AnonymousSurvey(question)
        # 创建包含三个答案的列表
        self.responses = ['English', 'Spanish', 'Mandarin']

    def test_store_single_response(self):
        """测试单个答案会被妥善的存储"""
        # 使用story_response() 方法存储单个答案 English
        self.my_survey.story_response('English')
        # 使用assertIn() 来核实单个答案包含在答案列表中
        self.assertIn('English', self.my_survey.responses)

    def test_story_three_responses(self):
        """测试三个答案会被妥善存储"""
        # 使用story_response() 方法存储responses中的三个答案
        for response in self.responses:
            self.my_survey.story_response(response)
        # 使用assertIn() 来核实三个答案包含在答案列表中
        for response in self.responses:
            self.assertIn(response, self.my_survey.responses)


unittest.main()

输出:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

进程已结束,退出代码 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值