我们能否在pytest中向fixture函数传递有条件的参数?

3
我想把一个列表作为参数传递给 fixture,根据 if 条件判断是否需要传递。有没有办法做到这一点?
举个例子,请看下面的代码。
我想要实现的是,如果我在 pytest 命令行参数中传递了 odd 作为 num 类型,那么作为参数传递给 fixture 的列表应该是奇数,而对于 even 参数则是偶数。
注:我正在使用 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
1个回答

0

这个答案基于我对问题 Filtering pytest fixtures先前回答

如果您需要一定程度的逻辑来确定要应用哪些参数到每个测试中,您可能希望考虑使用 pytest_generate_tests 钩子函数

pytest_generate_tests 钩子函数会为每个收集到的测试调用。 metafunc 参数允许您动态地为每个单独的测试用例设置参数。将 test_file.py 重写以使用 pytest_generate_tests 可以像这样:

import sys

def pytest_generate_tests(metafunc):
    if "fixture_function" in metafunc.fixturenames:
        parameters = []
        if metafunc.config.getoption("type") == "odd":
            parameters += [1, 3]
        elif metafunc.config.getoption("type") == "even":
            parameters += [2, 4]
        metafunc.parametrize("fixture_function", parameters)

def test_function(fixture_function):
    print(fixture_function, file=sys.stderr)

结合您现有的conftest.py代码,甚至可以通过--type命令行选项控制奇怪的测试参数:

$ pytest -v test_file.py --type odd
===== test session starts =====
…
collected 2 items                                                                                                                                                                                                                      

test_file.py::test_function[1] PASSED                                                                                                                                                                                        [ 50%]
test_file.py::test_function[3] PASSED                                                                                                                                                                                        [100%]

===== 2 passed in 0.02s =====


$ pytest -v test_file.py --type even
===== test session starts =====
…
collected 2 items                                                                                                                                                                                                                      

test_file.py::test_function[2] PASSED                                                                                                                                                                                        [ 50%]
test_file.py::test_function[4] PASSED                                                                                                                                                                                        [100%]

===== 2 passed in 0.01s =====

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接