pytest参数化多种用法总结

pytest.mark.parametrizepytest 的一个核心功能,它允许你参数化测试函数,这样你就可以使用不同的参数运行同一个测试函数多次。以下是 pytest.mark.parametrize 的详细用法总结:

基本用法

parametrize 装饰器可以接受一个或多个参数名,然后是一个参数值的列表(或列表的列表,如果测试函数有多个参数)。

python 复制代码
import pytest
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

在这个例子中,test_eval 函数将被调用三次,每次使用 test_inputexpected 参数的不同组合。

使用单个参数

如果你只需要参数化一个参数,可以只提供一个参数名和一个参数值的列表。

python 复制代码
@pytest.mark.parametrize("number", [1, 2, 3])
def test_square(number):
    assert number ** 2 == number * number

使用多个参数

如果你的测试函数需要多个参数,你可以提供一个包含多个参数的元组列表。

python 复制代码
@pytest.mark.parametrize("x, y", [(1, 2), (2, 3), (3, 4)])
def test_add(x, y):
    assert x + y == y + x

使用间接参数化

间接参数化允许你使用一个参数的值来决定另一个参数的值。

python 复制代码
@pytest.fixture(scope="module")
def input_value():
    return [1, 2, 3]
@pytest.mark.parametrize("indirect_param", indirect=True)
@pytest.mark.parametrize("input_value", [1, 2, 3], indirect=True)
def test_indirect(input_value, indirect_param):
    assert input_value == indirect_param

在这个例子中,input_value 参数的值会传递给 indirect_param

使用参数名

你可以使用参数名来指定参数的顺序。

python 复制代码
@pytest.mark.parametrize("a, b, expected", [(1, 2, 3), (2, 3, 5)])
def test_add(a, b, expected):
    assert a + b == expected

使用 ids 参数

你可以为每个参数组合提供一个唯一的标识符,这在测试结果输出中很有用。

python 复制代码
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6)], ids=["add", "add"])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

使用 indirect 参数

indirect 参数允许你将参数传递给 fixture 函数。

python 复制代码
@pytest.fixture
def arg(request):
    return request.param
@pytest.mark.parametrize("arg", [1, 2, 3], indirect=True)
def test_indirect(arg):
    assert arg > 0

使用 scope 参数

scope 参数允许你控制参数化 fixture 的作用域。

python 复制代码
@pytest.fixture(scope="module")
def arg(request):
    return request.param
@pytest.mark.parametrize("arg", [1, 2, 3], indirect=True, scope="module")
def test_indirect(arg):
    assert arg > 0

组合使用 parametrize 装饰器

你可以组合使用多个 parametrize 装饰器来创建更复杂的参数化测试。

python 复制代码
@pytest.mark.parametrize("a", [1, 2])
@pytest.mark.parametrize("b", [10, 20])
def test_foo(a, b):
    assert a < b

在这个例子中,test_foo 函数将被调用四次,因为每个 a 值都将与每个 b 值组合。

使用 marks 参数

你可以使用 marks 参数来应用多个标记到一个参数化测试上。

python 复制代码
@pytest.mark.parametrize("arg", [1, 2], marks=pytest.mark.xfail)
def test_xfail(arg):
    assert arg == 2

在这个例子中,所有的参数化测试都将被标记为 xfail

这些是 pytest.mark.parametrize 的基本用法。它是一个非常灵活和强大的功能,可以用于创建详尽的测试套件,确保代码在不同的输入下都能正确工作。

相关推荐
KoiHeng11 分钟前
操作系统简要知识
linux·笔记
Johny_Zhao4 小时前
Docker + CentOS 部署 Zookeeper 集群 + Kubernetes Operator 自动化运维方案
linux·网络安全·docker·信息安全·zookeeper·kubernetes·云计算·系统运维
小毛驴8504 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
好好学习啊天天向上5 小时前
世上最全:ubuntu 上及天河超算上源码编译llvm遇到的坑,cmake,ninja完整过程
linux·运维·ubuntu·自动性能优化
tan180°6 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
典学长编程7 小时前
Linux操作系统从入门到精通!第二天(命令行)
linux·运维·chrome
wuk9987 小时前
基于MATLAB编制的锂离子电池伪二维模型
linux·windows·github
snoopyfly~10 小时前
Ubuntu 24.04 LTS 服务器配置:安装 JDK、Nginx、Redis。
java·服务器·ubuntu
独行soc10 小时前
#渗透测试#批量漏洞挖掘#HSC Mailinspector 任意文件读取漏洞(CVE-2024-34470)
linux·科技·安全·网络安全·面试·渗透测试
BD_Marathon10 小时前
Ubuntu下Tomcat的配置
linux·ubuntu·tomcat