pytest.fixture可以将参数传递给另一个fixture的params吗?

问题描述 投票:0回答:1

使用固定装置作为另一个固定装置的参数,使用参数失败:

E TypeError:“函数”对象不可迭代

但是使用普通函数返回值就可以了。 这很好用:

@pytest.fixture(params=data_func())

失败了:

@pytest.fixture(params=data_func())

注意这是一个示例。我的实际脚本读取位于包根目录中的文件。我想通过

pytestconfig.rootpath
检索包根目录。目前,我使用环境变量解决了这个问题,但我宁愿不必这样做,因为我必须为每个项目提供不同的变量名称。如果可能的话,最好从 pytest 中检索它。我很欣赏对此的任何想法。谢谢。

此脚本使用示例输入生成测试列表:

[('mistral', 'func1'), ('mistral', 'func2'), ('llama2', 'func1'), ('llama2', 'func2')]

以下脚本运行良好:

import pytest


def data_func():
    models = [
        "mistral",
        "llama2",
    ]
    funcs = [
        "func1",
        "func2",
    ]
    mylist = [(m, f) for m in models for f in funcs]
    print(mylist)
    return mylist

@pytest.fixture(params=data_func())
def myargs(request):
    yield request.param

def test_a(myargs):
    model, func = myargs
    print(f"model {model} func {func}")
 testings/testings6/tests/ques1.py ✓                                                                                                         25% ██▌       model mistral func func1
 testings/testings6/tests/ques1.py ✓✓                                                                                                        50% █████     model llama2 func func1
 testings/testings6/tests/ques1.py ✓✓✓                                                                                                       75% ███████▌  model llama2 func func2
 testings/testings6/tests/ques1.py ✓✓✓✓                                                                                                     100% ██████████

但是使用固定装置作为参数的输入使用yield语句失败了:

import pytest


@pytest.fixture
def data_fixture():
    models = [
        "mistral",
        "llama2",
    ]

    funcs = [
        "func1",
        "func2",
    ]
    mylist = [(m, f) for m in models for f in funcs]
    return mylist

@pytest.fixture(params=data_fixture)
def myargs(request):
    print(request)
    #yield request.param
    yield request.getfixturevalue(request.param)

def test_a(myargs):
    model, func = myargs
    print(f"model {model} func {func}")

        pytest     = <module 'pytest' from '/home/kenneth/learning/venv_latest/lib/python3.10/site-packages/pytest/__init__.py'>
../../../venv_latest/lib/python3.10/site-packages/_pytest/fixtures.py:1324: in fixture
    params=tuple(params) if params is not None else None,
E   TypeError: 'function' object is not iterable
python pytest pytest-fixtures
1个回答
0
投票

一些评论

  1. “这有效”和“这失败”行对我来说看起来相同
  2. 夹具不是适合这项工作的工具,您需要参数化

这里有一个建议:

import pytest

@pytest.mark.parametrize("func", ["func1", "func2"])
@pytest.mark.parametrize( "model", ["mistral", "llama2"] )
def test_a(model, func):
    print(f"model {model} func {func}")

输出

test_me.py::test_a[mistral-func1] PASSED                  [ 25%]
test_me.py::test_a[mistral-func2] PASSED                  [ 50%]
test_me.py::test_a[llama2-func1] PASSED                   [ 75%]
test_me.py::test_a[llama2-func2] PASSED                   [100%]
© www.soinside.com 2019 - 2024. All rights reserved.