【发布时间】:2016-05-26 14:03:11
【问题描述】:
我只是想了解如果我在 pytest.mark.parametrize 中将间接参数设置为 True 或 False 是什么意思或会发生什么?
谢谢
【问题讨论】:
-
stackoverflow.com/questions/18011902/… 用于向夹具传递参数
我只是想了解如果我在 pytest.mark.parametrize 中将间接参数设置为 True 或 False 是什么意思或会发生什么?
谢谢
【问题讨论】:
使用indirect=True,您可以参数化您的灯具,False - 默认值。示例:
import pytest
@pytest.fixture
def fixture_name(request):
return request.param
@pytest.mark.parametrize('fixture_name', ['foo', 'bar'], indirect=True)
def test_indirect(fixture_name):
assert fixture_name == 'baz'
所以这个例子生成了两个测试。第一个来自fixture_name 值foo,因为这个测试的fixture 使用参数化运行。第二个测试获得 bar 值。并且每个测试都会失败,因为断言检查 baz。
【讨论】:
indirect 是一种参数化夹具的方法。与使用 pytest.fixture(params=[...]) 参数化夹具相比的主要区别在于,夹具只能针对特定的测试功能进行参数化(否则它总是为使用夹具的所有测试功能进行参数化)?
request。其他名称将不起作用,pytest 会抱怨该其他名称的夹具不存在。 pytest 6.2.2