【问题标题】:Can we pass conditional parameters to fixture functions in pytest?我们可以将条件参数传递给 pytest 中的夹具函数吗?
【发布时间】: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


    【解决方案1】:

    此答案基于我的previous anwser 问题Filtering pytest fixtures

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

    每个收集到的测试都会调用钩子函数pytest_generate_testsmetafunc 参数允许您动态参数化每个单独的测试用例。重写 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.pycode,甚至可以通过--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 =====
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 2015-09-23
      相关资源
      最近更新 更多