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

相关推荐
longerxin20203 小时前
在 Linux 上使用 SCP 将文件传输到 Windows(已开启 SSH)
linux·运维·ssh
王正南6 小时前
kali-linux 虚拟机连接安卓模拟器
android·linux·运维·虚拟机连接模拟器·安卓模拟器,linux虚拟机
Web极客码6 小时前
如何在Ubuntu服务器上安装和配置BIND9
服务器·数据库·ubuntu
吳所畏惧6 小时前
Linux环境/麒麟V10SP3下离线安装Redis、修改默认密码并设置Redis开机自启动
linux·运维·服务器·redis·中间件·架构·ssh
yueguangni7 小时前
sysstat 版本 10.1.5 是 CentOS 7 的默认版本,默认情况下确实不显示 %wait 字段。需要升级到新版sysstat
linux·运维·centos
西***63477 小时前
全兼容・高安全:KVM 一站式服务器远程监控与管理指南
服务器
萧曵 丶8 小时前
Linux 业务场景常用命令详解
linux·运维·服务器
豆是浪个9 小时前
Linux(Centos 7.6)命令详解:ps
linux·windows·centos
Run_Teenage9 小时前
Linux:深刻理解缓冲区
linux
youxiao_9010 小时前
kubernetes 概念与安装(一)
linux·运维·服务器