Python 使用pytest.lazy_装置列表值作为另一装置中的参数
我正在尝试使用一个装置值列表作为另一个装置的参数。以下是我的设置:Python 使用pytest.lazy_装置列表值作为另一装置中的参数,python,automated-tests,pytest,fixtures,Python,Automated Tests,Pytest,Fixtures,我正在尝试使用一个装置值列表作为另一个装置的参数。以下是我的设置: import pytest d = { "first": [1, 2, 3], "second": [4, 5, 6] } @pytest.fixture(params=['first', 'second']) def foo(request): return d.get(request.param) @pytest.fixture(params=[pytest.lazy_fixture('foo'
import pytest
d = {
"first": [1, 2, 3],
"second": [4, 5, 6]
}
@pytest.fixture(params=['first', 'second'])
def foo(request):
return d.get(request.param)
@pytest.fixture(params=[pytest.lazy_fixture('foo')])
def bar(request):
return request.param
def test_1(bar):
pass
问题是bar()
始终以请求的形式获取完整列表。param
([1,2,3]不是列表的值。如果在bar()的params
中,夹具直接发送数据,例如:
@pytest.fixture(params=[1, 2, 3])
def bar(request):
return request.param
def test_1(bar):
pass
然后参数请求将正常工作(测试开始三次)。同样的情况,如果我不是直接将参数传递给params
,而是从没有fixture decorator的任何方法传递参数,即:
def some():
return [1, 2, 3]
@pytest.fixture(params=some())
def more(request):
return request.param
def test_2(more):
logging.error(more)
pass
所以,我的问题是,是否可以从列表中逐个获取数据,然后在测试中使用它?我已尝试“取消分析”列表:
@pytest.fixture(params=[i for i in i pytest.lazy_fixture('foo')])
def bar(request):
return request.param
但在本例中,我得到了TypeError:“LazyFixture”对象是不可编辑的
请参见:根据设计,夹具值不能用作参数列表来对测试进行参数化。实际上,参数是在pytest收集阶段解析的,而夹具是在pytest节点执行期间稍后解析的
使用pytest\u案例或使用pytest\u案例可以做的最好的事情。fixture\u ref
是使用单个fixture值作为参数。使用lazy\u fixture
你有一些限制(如果我没记错的话,fixture无法参数化),而使用fixture你几乎可以做任何事情(在参数元组中使用fixture\u ref
,使用几个fixture\u ref
s,每个都有不同的参数化,等等)。顺便说一下,我是pytest\u案例的作者;)
另请参见。grmmvv,您是否找到了解决上述问题的方法?