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

相关推荐
zl_dfq24 分钟前
Linux 之 【进程信号】(signal、kill、raise、abort、alarm、Core Dump核心转储机制)
linux
Ankie Wan38 分钟前
cgroup(Control Group)是 Linux 内核提供的一种机制,用来“控制、限制、隔离、统计”进程对系统资源的使用。
linux·容器·cgroup·lxc
skywalk81631 小时前
尝试在openi启智社区的dcu环境安装ollama最新版0.15.2(失败)
linux·运维·服务器·ollama
看我干嘛!1 小时前
第三次python作业
服务器·数据库·python
我的xiaodoujiao1 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--将自动化测试结果自动推送至钉钉工作群聊
前端·python·测试工具·ui·pytest
Andy Dennis1 小时前
FTP局域网功能小网站V2_2
服务器·flask·html5
zhengfei6112 小时前
AutoPentestX – Linux 自动化渗透测试和漏洞报告工具
linux·运维·自动化
我材不敲代码2 小时前
在Linux系统上安装MySQL
linux·运维·服务器
yuezhilangniao2 小时前
阿里云服务器Alibaba Cloud Linux 3 安装Python3.11简明指南
linux·运维·python3.11
珠海西格2 小时前
远动通信装置为何是电网安全运行的“神经中枢”?
大数据·服务器·网络·数据库·分布式·安全·区块链