【发布时间】:2018-07-28 00:28:59
【问题描述】:
我想根据 if 条件将列表作为参数传递给夹具。有没有办法做到这一点?
例如,请看下面的代码。
我想要实现的是,如果我在 pytest 的命令行参数中将奇数作为 num 类型传递,则作为参数传递给夹具的列表应该是奇数,偶数的参数应该是偶数。
P.S 我使用 pytest-xdist 多次运行此测试,因此我使用 sys.stderr 打印输出。
文件名:conftest.py
def pytest_addoption(parser):
parser.addoption('--type', action='store', help='Number of times to repeat each test')
文件名:test_file.py
import pytest, sys
lis = []
@pytest.fixture()
def nested_fixture(pytestconfig):
global lis
numtype = str(pytestconfig.getoption("type"))
if numtype == "odd":
lis.append(1)
lis.append(3)
if numtype == "even":
lis.append(2)
lis.append(4)
return lis
@pytest.fixture(params = nested_fixture)
def fixture_function(request):
return request.param
def test_function(fixture_function):
print >> sys.stderr , fixture_function
我在终端中使用以下命令执行此操作
pytest -sv -n 2 --type odd
【问题讨论】:
标签: python testing pytest fixtures