pytest Python单元测试框架用法

特此声明:本文参考自 [1] Pajankar A . Python Unit Test Automation[M]. Apress, 2017.

1. pytest

nose 是一个比起过时的、基础的 unittest 更好的、高级的 Python 测试框架,
然而,nose 已经没有处于活跃的开发中,nose 用户无法得到软件功能的相应提升,维护以及支持,不应该作为 Python 开发活动的首选测试框架。
作为过时的、基础的 unittest 测试运行器,nose2 也没有处于活跃的开发中,同样已经过时了。
作为一个 Python 程序开发者,势必需要一个比起 unittest更好的 Python 测试框架,以及处于活跃的开发状态中的 Python 测试框架。

本文介绍 python 测试框架 pytest,其具有技术流行的、开发活跃的、性能高级的优点。

1.1. 安装步骤

  1. 命令提示符键入以下命令:
pip install pytest

图2.1.
图1.1.1. 卸载与安装py.test
2. 通过以下命令可以查看安装结果

py.test --version

1.2. 简易测试

def test_case01():
    assert "helloworld".upper() == "HELLOWORLD"
  1. 键入以下CMD命令行输出精简信息
python -m pytest test_model01.py
py.test test_module01.py
  1. 键入以下CMD命令行输出冗余信息
python -m pytest -v  test_model01.py
py.test test_module01.py -v

在这里插入图片描述

图 1.2.1. 左边与右边命令行分别输出精简信息以及冗余信息
注意: pytest 输出测试内容自身是彩色的。

1.3. 类与方法的测试

  • 第1步,创建测试类
class TestClass01:
    def test_case01(self):
        assert 'helloworld'.upper() == 'HELLOWORLD'
    def test_case02(self):
        assert 'HELLOWORLD'.lower() == 'helloworld'
  • 第2步, 创建_init.py 文件,将所要测试的文件包含在内;本文中即为 test_module01 以及 test_module02
all = ["test_module01", "test_module02"]
  • 第3步,在测试文件夹的父文件夹内,使用 pytest 相关命令执行测试功能,测试的范围可调,范围由大到小依次是:包(文件夹),测试模块,测试类,测试类中的方法,测试函数
py.test test  
py.test -v test
py.test -v test/test_module01.py
py.test -v test/test_module02.py::TestClass01
py.test -v test/test_module02.py::TestClass01::test_case01
py.test -v test/test_module01.py::test_case01

在这里插入图片描述
另外,类似常规的unittest、nose测试工具,pytest 能够发现和自动运行测试。

  • 第3步*,也可以在测试文件夹中,运行以下命令也可以执行包(文件夹)测试功能
py.test
py.test -v

在这里插入图片描述
图 1.3.2. 命令行输出精简信息 & 冗余信息

1.3. xUnit-Style 测试夹具

1.3.1. xUnit-Style 测试夹具作用域

名称层级调用时间
setup_module() & teardown_module()模块在模块中的任何其他行为之前和之后都会调用。
setup_class() & teardown_class()在类中的任何其他行为之前和之后都会调用,需要使用@classmethod()修饰符标志。
setup_method() &t eardown_method()方法在每种测试方法测试方法之前和之后运行。
setup_function() & teardown_function()函数在系统中的每个测试函数的之前和之后运行。
  • 运行以下命令查看上述测试夹具运行细节的日志
py.test -vs test_module03.py 
py.test -v test_module03.py

在这里插入图片描述
py.test 支持 unittest编写的xUnit-Style (x单元测试风格)测试夹具以及nose编写的大多数测试夹具,此外,py.test 还提供自定义的测试夹具。

1.4. py.test 自定义测试夹具

  1. 输入以下命令建立针对函数的自定义测试夹具
import pytest


@pytest.fixture()
def fixture01():
    print("\nIn fixture01()...")


def test_case01(fixture01):
    print("\nIn test_case01()...")

  1. 输入以下命令建立针对测试类的自定义测试夹具
import pytest


@pytest.fixture()
def fixture01():
    print("\nIn fixture01()...")


@pytest.mark.usefixtures('fixture01')
class TestClass03:
    def test_case01(self):
        print("I'm the test_case01")


    def test_case02(self):
        print("I'm the test_case02")

在这里插入图片描述
3. 输入以下命令建立针对测试类的自定义测试夹具

import pytest


@pytest.fixture()
def fixture01():
    print("\nIn fixture01()...")


@pytest.mark.usefixtures('fixture01')
class TestClass03:
    def test_case01(self):
        print("I'm the test_case01")

    def test_case02(self):
        print("I'm the test_case02")
  1. 输入以下命令添加具有终结器的、基于函数的自定义测试夹具
import pytest


@pytest.fixture()
def fixture01(request):
    print("\nIn fixture...")

    def fin():
        print("\nFinalized...")
    request.addfinalizer(fin)


@pytest.mark.usefixtures('fixture01')
def test_case01():
    print("\nI'm the test_case01")

在这里插入图片描述

1.4.1. 自定义夹具作用域

类似 xUnit-Style 夹具,py.test 的作用域可以通过 @pytest.fixture(scope=“class”)中scope的值进行指定。默认情况下,scope的值为function,函数测试夹具将分配给所有测试。

scope 值作用域
function每个测试运行一次
class每个测试类运行一次
module每个模块运行一次
session每个会话运行一次

下面示例将展示默认情况下测试夹具的作用域。
在这里插入图片描述

进一步阅读

通过在命令行提示符键入下方命令可以获得关于py.test的进一步帮助。

py.test -h
py.test --help

参考文献

PEP 8 惯例
xUnit 模式

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值