我们可以在pytest中将条件参数传递给Fixture函数吗?

时间:2018-07-28 00:28:59

标签: python testing pytest fixtures

我想基于if条件将列表作为参数传递给Fixture。有办法吗?

例如,请参见以下代码。

我想要实现的是,如果我在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

1 个答案:

答案 0 :(得分:0)

此答案基于我对问题 previous anwserFiltering pytest fixtures

如果您需要一定程度的逻辑来确定将哪些参数应用于每个测试,您可能需要考虑使用 pytest_generate_tests hook.

钩子函数 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 =====
相关问题