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

相关推荐
꧁坚持很酷꧂29 分钟前
Linux Ubuntu18.04下安装Qt Craeator 5.12.9(图文详解)
linux·运维·qt
凉、介1 小时前
PCI 总线学习笔记(五)
android·linux·笔记·学习·pcie·pci
电鱼智能的电小鱼1 小时前
EFISH-SBC-RK3588无人机地面基准站项目
linux·网络·嵌入式硬件·机器人·无人机·边缘计算
电鱼智能的电小鱼1 小时前
基于 EFISH-SBC-RK3588 的无人机环境感知与数据采集方案
linux·网络·嵌入式硬件·数码相机·无人机·边缘计算
小诸葛的博客2 小时前
详解Linux中的定时任务管理工具crond
linux·运维·chrome
一默19912 小时前
CentOS 7.9升级OpenSSH到9.9p2
linux·运维·centos
keep intensify2 小时前
Linux常用指令
linux·服务器·php
带电的小王3 小时前
sherpa-ncnn:Linux(x86/ARM32/ARM64)构建sherpa-ncnn --语音转文本大模型
linux·语音识别·实时音视频·sherpa-ncnn
沧浪之水!3 小时前
【Linux网络】:套接字之UDP
linux·网络·udp
BranH3 小时前
Linux系统中命令设定临时IP
linux·运维·服务器