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'

执行结果

相关推荐
ACGkaka_9 分钟前
MySQL 学习(十)执行一条查询语句的内部执行过程、MySQL分层
学习·mysql·adb
南部余额1 小时前
Python 类变量与实例变量完全指南:区别、使用场景及常见陷阱
开发语言·python
yunvwugua__1 小时前
Python训练营打卡 Day26
前端·javascript·python
满怀10151 小时前
【Django全栈开发实战】从零构建企业级Web应用
前端·python·django·orm·web开发·前后端分离
半路_出家ren1 小时前
python处理异常,JSON
python·json·异常处理
仙人掌_lz1 小时前
深度理解用于多智能体强化学习的单调价值函数分解QMIX算法:基于python从零实现
python·算法·强化学习·rl·价值函数
小白学大数据1 小时前
Python+Selenium爬虫:豆瓣登录反反爬策略解析
分布式·爬虫·python·selenium
未来之窗软件服务1 小时前
人体肢体渲染-一步几个脚印从头设计数字生命——仙盟创梦IDE
开发语言·ide·人工智能·python·pygame·仙盟创梦ide
戌崂石2 小时前
最优化方法Python计算:有约束优化应用——线性不可分问题支持向量机
python·机器学习·支持向量机·最优化方法
玉笥寻珍2 小时前
Web安全渗透测试基础知识之内存动态分配异常篇
网络·python·安全·web安全·网络安全