带参数的最热灯具

2022-03-01 00:00:00 python pytest fixtures

问题描述

似乎可以将参数传递给fixture:

Pass a parameter to a fixture function

但是,在实现此最小示例时,我收到错误。

import pytest

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

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

@pytest.fixture 定义我的装置(V)(_F): 找不到设备"%v"

上面的代码有什么问题吗?

(python 3.8.10,pytest-6.2.5)


解决方案

工作正常:

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

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


相关文章