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 的基本用法。它是一个非常灵活和强大的功能,可以用于创建详尽的测试套件,确保代码在不同的输入下都能正确工作。

相关推荐
一个响当当的名号1 小时前
一些主要应用和NAT
运维·服务器·网络
@小博的博客1 小时前
【Linux探索学习】第二篇Linux的基本指令(2)——开启Linux学习第二篇
linux·运维·学习
openHiTLS密码开源社区3 小时前
【密码学实战】openHiTLS passwd命令行:专业密码哈希生成工具
linux·密码学·哈希算法·ldap·密码策略·随机盐值
筵陌3 小时前
深入理解 Reactor 反应堆模式:高性能网络编程的核心
服务器
WTCLLB4 小时前
netgear r6220 路由器,刷openwrt后,系统备份还原
linux·网络·智能路由器·openwrt
迎風吹頭髮4 小时前
UNIX下C语言编程与实践38-UNIX 信号操作:signal 函数与信号捕获函数的编写
linux·c语言·unix
做运维的阿瑞5 小时前
Linux系统性能监控与故障定位实战:CPU/内存/I/O/网络
linux·运维·网络
驱动探索者5 小时前
车库到双子星:惠普的百年科技传奇
linux
阿沁QWQ5 小时前
MySQL服务器配置与管理
服务器·数据库·mysql
wanhengidc5 小时前
云手机能够做些什么?
运维·服务器·人工智能·智能手机·云计算