python - 我们可以将条件参数传递给 pytest 中的 fixture 函数吗?

标签 python testing pytest fixtures

我想根据 if 条件将列表作为参数传递给 fixture。有办法做到这一点吗?

例如,请看下面的代码。

我想要实现的是,如果我在 pytest 的命令行参数中将 odd 作为 num 类型传递,则作为参数传递给 fixture 的列表应该是奇数,对于参数 even 应该是偶数。

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

最佳答案

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

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

钩子(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 =====

关于python - 我们可以将条件参数传递给 pytest 中的 fixture 函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51567032/

相关文章:

python - py.test 将一个测试的结果传递给另一个

python - 如何使用 `fixture` 和 `parametrize` 为 pytest 测试设置环境变量

Python/Tkinter : Using custom mouse cursors under Windows?

python - python中的张量点运算

testing - Flutter - 如何在小部件测试中获取文本小部件

python - 如何从 pytest_addoptions 访问 pytest conftest 中的命令行输入并在 fixture 参数中使用它?

python - python 将包含日期时间对象的记录插入 mysql 数据库时出现问题

python - Pandas+seaborn 多维数据框分面

ruby-on-rails - 如何避免 Rspec 共享示例 'previously defined' 警告?

java - 等待图表刷新,selenium Java