Python之unittest基础用法

Python在编写测试案例方面较为方便快速,有利于日常测试工作的开展和维护,Python自带的【unittest】模块是一个设计简介、高效上手的框架,可以满足大部分测试场景。

unittest的设计思想也很容易理解,分为四大组件:test case测试案例、test suite测试组件、test fixture测试固定设施、test runner测试执行器。

麻雀虽小五脏俱全,为你一一道来。

测试,需要建立测试案例,用来验证每一种测试场景,因此设计了test case。实现方式就是集成【TestCase】类,然后编写一个个独立的以【test_】开头的测试方法,这些方法就是印证每一种测试场景。

当编写的测试案例越来越多的时候,代码会变得难以【管理】,因此需要建立组件、套件,来规范化,将测试案例更好的管理起来,这就设计了test case。实现方式就是创建【TestSuite】实例,然后装载指定的【TestCase】类集合,形成一个个的套件。日复一日,你会发现,测试案例再多,也可以很快速的将他们管理起来。

在编写测试案例的过程中,你会发现很多东西是重复利用的,比如建立数据库连接,大多数测试场景都要用到,那么我们不可能复制一份代码来重用吧?这时候就需要设计test fixture测试固定设施,编写【setUp】、【tearDown】来进行初始化工作、清理工作,这样就可以最少的代码进行【复用】这些相同的功能。

最后,书写完这些测试工作,需要一个执行器来执行,就需要设计test runner测试执行器,这个部分相对简单,就是将上面的案例、套件装载进来,使用【TestRunner】实例就可以运行了。

好好学习,天天向上,示例代码如下:

from unittest import TestSuite, TestCase, main, TextTestRunner, TestLoader

# 官网 https://docs.python.org/3/library/unittest.html
# 测试工具大全 https://wiki.python.org/moin/PythonTestingToolsTaxonomy

"""
官方文档

test fixture
A test fixture represents the preparation needed to perform one or more tests, and any associated cleanup actions.
This may involve, for example, creating temporary or proxy databases, directories, or starting a server process.

test case
A test case is the individual unit of testing. It checks for a specific response to a particular set of inputs.
unittest provides a base class, TestCase, which may be used to create new test cases.

test suite
A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.

test runner
A test runner is a component which orchestrates the execution of tests and provides the outcome to the user.
The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.
"""

"""
解析

test fixture 测试的固定设施
主要负责测试案例的【准备工作】和【清理工作】,因为测试案例写多了之后,会有很多相同的前后工作,比如创建临时的或者代理的数据库、文件夹,还有开启服务进程、线程等操作

test case 测试的案例
表示一个独立的,与外界无关的测试案例,只在特定的输入之后,检查特定的响应结果。
框架提供了【TestCase】类,继承它就可以创建测试案例

test suite 测试组件
测试组件,用于收集多个 test case、test suite,这样就可以达到一起运行的目的,让测试案例得以分类,更好的【管理】。

test runner 测试执行器
【执行器】属于执行测试案例的部分,并提供测试结果给到用户。
可以使用可视化界面、文本接口或者返回特定的值,来表示测试结果。
"""


# 第一个测试类
class MyCase1(TestCase):
    # 多个独立的测试案例
    def test_1(self):
        self.assertEqual(1, 1, msg='1 == 1')
        print('test_1 done')

    def test_2(self):
        self.assertEqual(1, 1, msg='1 == 1')
        print('test_2 done')

    # 设置
    def setUp(self) -> None:
        print('\033[1;37;41m start:初始化工作,比如建立数据库连接、读取文件内容等 \033[0m ')

    # 拆卸
    def tearDown(self) -> None:
        print('\033[1;37;41m end:清理工作,比如销毁数据库连接、删除缓存等 \033[0m ')


# 第二个测试类
class MyCase2(TestCase):
    # 多个独立的测试案例
    def test_1(self):
        self.assertEqual(1, 1, msg='1 == 1')
        print('test_1 done')

    def test_2(self):
        self.assertEqual(1, 1, msg='1 == 1')
        print('test_2 done')

    # 设置
    def setUp(self) -> None:
        print('\033[1;37;41m start:初始化工作,比如建立数据库连接、读取文件内容等 \033[0m ')

    # 拆卸
    def tearDown(self) -> None:
        print('\033[1;37;41m end:清理工作,比如销毁数据库连接、删除缓存等 \033[0m ')


# 组装测试套件
def suite(cases):
    _suite = TestSuite()
    _loader = TestLoader()
    for test_class in cases:
        tests = _loader.loadTestsFromTestCase(test_class)
        _suite.addTests(tests)
    return _suite


# 第一种执行方式,执行加载过的所有的【TestCase】
def run1():
    main()


# 第二种执行方式,使用执行器,执行指定的测试类
# 推荐
def run2():
    TextTestRunner().run(suite([MyCase1, MyCase2]))


if __name__ == '__main__':
    # run1()
    run2()

运行结果

小小的心得

感觉好简单是吧?这个东西确实挺简单的,四个组件就讲完了,case、suite、fixture和runner,分别是案例、套件、固定设施和执行器。往后的工作中还是要熟悉具体的api方法用法,将来编写测试案例的时候就比较容易上手。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值