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

news/2024/9/23 3:37:19/

文章目录

      • 官方概念
      • 概念分析
      • 官方示例
      • 示例分析
      • 验证indirect为True但不指定fixture
      • 验证indirect为True但不存在fixture


请添加图片描述

官方概念

请添加图片描述

概念分析

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'

执行结果

请添加图片描述


http://www.ppmy.cn/news/1426072.html

相关文章

Qt gsl库配置踩坑记录

想求解非线性方程组,之前使用拟牛顿法写过相关的matlab代码,这次想移植到C代码,网上说gsl库挺好用的,于是我也想试一下。相关参考: 【C】GSL(GNU Scientific Library) 的安装及在 Visual Studio 2017 中的使用 QT5使用…

getOutputStream() has already been called for this response

问题描述 在做java导出Excel数据的时候,接口层面需要有HttpServletResponse的入参来设置输出流 然后执行的时候报getOutputStream() has already been called for this response错误 问题排查 返回的错误信息 {"timestamp": "2024-04-16T11:49:…

比特币减半倒计时:NFT 生态将受到怎样的影响?

BTC 减半倒计时仅剩不到 1 天,预计在 4 月 20 日迎来减半。当前区块奖励为 6.25 BTC,减半后区块奖励为 3.125 BTC,剩余区块为 253。比特币减半无疑是比特币发展史上最重要的事件之一,每当这一事件临近,整个加密社区都充…

蓝桥杯2024年第十五届省赛真题-小球反弹

以下两个解法感觉都靠谱&#xff0c;并且网上的题解每个人答案都不一样&#xff0c;目前无法判断哪个是正确答案。 方法一&#xff1a;模拟 代码参考博客 #include <iostream> #include <cmath> #include <vector>using namespace std;int main() {const i…

Spring Boot入门(17):秒懂Spring Boot整合Knife4j,让你的Swagger界面秒变高颜值

前言 在使用Swagger进行API文档编写时&#xff0c;我们不可避免的会遇到Swagger的一些瓶颈。例如&#xff0c;Swagger的UI界面不太友好&#xff0c;样式单调且难看&#xff0c;交互体验也不是很好。为了解决这些问题&#xff0c;我们可以使用Knife4j对Spring Boot进行整合&…

【SQL】DISTINCT GROUP BY

找到所有办公室里的所有角色&#xff08;包含没有雇员的&#xff09;,并做唯一输出(DISTINCT) 用DISTINCT : SELECT DISTINCT B.Building_name,E.Role FROM Buildings B LEFT JOIN Employees EON B.Building_name E.Building需要找到的结果&#xff1a;所有办公室名字&#…

途游游戏,科锐国际(计算机类),快手,得物,蓝禾,奇安信,顺丰,康冠科技,金证科技24春招内推

途游游戏&#xff0c;科锐国际&#xff08;计算机类&#xff09;&#xff0c;快手&#xff0c;得物&#xff0c;蓝禾&#xff0c;奇安信&#xff0c;顺丰&#xff0c;康冠科技&#xff0c;金证科技24春招内推 ①得物 【岗位】技术&#xff0c;设计&#xff0c;供应链&#xff0…

echarts柱状图动态排序

一、前言 echarts在前端开发中实属必不可缺的大数据可视化工具&#xff0c;在前段时间搬砖时遇到这样一个需求&#xff0c;需要实现一个动态排序的柱状图&#xff0c;用来展示不同部门在不同月份的绩效收益情况&#xff0c;能够直观地看到每个月各个部门的排名变化情况。并能够…