You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pytest fixture传参时报fixture 'v' not found错误如何解决

错误原因

  • 使用indirect=True向fixture传递参数时,fixture无法通过自定义参数名(你代码里的v)接收入参,pytest会将这个自定义参数识别为独立的fixture,找不到对应fixture就会抛出你遇到的报错。
  • 正确的传参接收方式是通过pytest内置的request fixture,你传递的参数会被存放在request.param属性中。
  • indirect=True的作用是告知pytest,@pytest.mark.parametrize中指定的参数名是fixture名称,入参会先传递给对应fixture处理,再返回给测试函数使用。

修正后代码

import pytest

@pytest.fixture
def my_fixture(request):
    print("fixture in")
    yield request.param + 1
    print("fixture out")

@pytest.mark.parametrize("my_fixture",[1], indirect=True)
def test_myfixture(my_fixture):
    print(my_fixture)

运行效果

测试运行正常,最终打印结果为2,fixture的进出日志也会正常输出。

内容的提问来源于stack exchange,提问作者Vince

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 02:06:05