Pytest精通指南(14)Parametrize之indirect(间接参数)


文章目录


官方概念

概念分析

pytest@pytest.mark.parametrize装饰器中,indirect参数用于指示是否应该从fixtures中解析参数值,而不是直接使用提供的值。

当 indirect=False(默认值)时:

  • argnames参数被当成普通变量处理;
  • argvalues中提供的值会直接作为这些变量的值。
  • 此时,argnames中不需要引用任何已定义的fixture

当 indirect=True 时:

  • argnames参数被当成fixture函数处理;
  • pytest会尝试从定义的fixtures中查找具有相同名称的fixture
  • 如果存在则将argvalues值作为argnames函数中的参数传参给这个fixture
  • 之后将fixture函数的返回值作为参数化数据,
  • 因此,必须先行定义与之匹配的fixture函数。

理解indirect参数的关键是将其视为一个开关,用于控制argnames的解析方式。

这种机制使得我们可以灵活地管理测试数据,将复杂的数据生成逻辑封装在fixtures中,并通过参数化测试来复用这些数据。

官方示例

示例代码

python 复制代码
import pytest


@pytest.fixture(scope='function')
def x(request):
    return request.param * 3


@pytest.fixture(scope='function')
def y(request):
    return request.param * 2


@pytest.mark.parametrize('x,y', [['a', 'b']], indirect=['x'])
def test_x(x, y):
    assert x == 'aaa'
    assert y == 'b'


@pytest.mark.parametrize('x,y', [['a', 'b']], indirect=['x','y'])
def test_x_y(x, y):
    assert x == 'aaa'
    assert y == 'bb'

执行结果

示例分析

示例代码

python 复制代码
import pytest


@pytest.fixture(scope='function')
def x(request):
    return request.param * 3


@pytest.fixture(scope='function')
def y(request):
    return request.param * 2


@pytest.mark.parametrize(argnames='x,y', argvalues=[['a', 'b']], indirect=['x'])
def test_x(x, y):
    print(f"x={x}, y={y}")
    assert x == 'aaa'
    assert y == 'b'


@pytest.mark.parametrize(argnames='x,y', argvalues=[['a', 'b']], indirect=['x', 'y'])
def test_x_y(x, y):
    print(f"x={x}, y={y}")
    assert x == 'aaa'
    assert y == 'bb'

执行结果

验证indirect为True但不指定fixture

示例代码

python 复制代码
import pytest


@pytest.fixture(scope='function')
def x(request):
    return request.param * 3


@pytest.fixture(scope='function')
def y(request):
    return request.param * 2


@pytest.mark.parametrize(argnames='x,y', argvalues=[['a', 'b']], indirect=['x'])
def test_x(x, y):
    print(f"x={x}, y={y}")
    assert x == 'aaa'
    assert y == 'b'


@pytest.mark.parametrize(argnames='x,y', argvalues=[['a', 'b']], indirect=True)
def test_x_y(x, y):
    print(f"x={x}, y={y}")
    assert x == 'aaa'
    assert y == 'bb'

执行结果

验证indirect为True但不存在fixture

示例代码

python 复制代码
import pytest


@pytest.mark.parametrize(argnames='x,y', argvalues=[['a', 'b']], indirect=['x'])
def test_x(x, y):
    print(f"x={x}, y={y}")
    assert x == 'aaa'
    assert y == 'b'


@pytest.mark.parametrize(argnames='x,y', argvalues=[['a', 'b']], indirect=True)
def test_x_y(x, y):
    print(f"x={x}, y={y}")
    assert x == 'aaa'
    assert y == 'bb'

执行结果

相关推荐
AIAdvocate1 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼1 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
铁匠匠匠1 小时前
从零开始学数据结构系列之第六章《排序简介》
c语言·数据结构·经验分享·笔记·学习·开源·课程设计
架构文摘JGWZ3 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
FreakStudio3 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
小齿轮lsl3 小时前
PFC理论基础与Matlab仿真模型学习笔记(1)--PFC电路概述
笔记·学习·matlab
Aic山鱼3 小时前
【如何高效学习数据结构:构建编程的坚实基石】
数据结构·学习·算法
qq11561487074 小时前
Java学习第八天
学习
天玑y4 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
2301_789985944 小时前
Java语言程序设计基础篇_编程练习题*18.29(某个目录下的文件数目)
java·开发语言·学习