如何传递使用夹具作为参数的函数进行参数化?

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

我有一个函数可以生成一个字典,我想在测试用例的参数化中使用它。但是,该函数需要一个固定装置(链接到一个类)来获取一些数据。当直接在参数化中调用此函数并将夹具用作参数时,会生成错误 -

'function' object is not iterable

def myFunction(fixtureToUse):
    res = fixtureToUse.class_function()
    # Use res to generate my dicitonary
    return myDict


@pytest.mark.parametrize('keyVal', myFunction(fixtureToUse))
def testMyTestCase(fixtureToUse, keyVal)
    # Execute testcase logic

我尝试了一些事情,例如:

  1. 直接在我的测试用例中调用它并使用 for 循环,这有效,但这不是我想要的。
  2. 从参数中删除了 pxtv_session 并尝试了
    usefixtures()
    但这不起作用,因为它创建了一个不同的错误,其中我的函数中的命令无法解析引用。
  3. 创建了一个类并尝试在运行实际测试用例之前使用我的函数来更新类变量,也许我不太了解类,但这也不起作用。
python pytest fixtures parametrized-testing
1个回答
0
投票

你可以使用

request.getfixturevalue()
:

import pytest


@pytest.fixture
def fix1():
    return 1


@pytest.fixture
def fix2():
    return 2


def make_dict(fx):
    return {"a": fx}


@pytest.mark.parametrize("fixture_name", ["fix1", "fix2"])
def test_a(request, fixture_name):
    some_fixture_value = make_dict(request.getfixturevalue(fixture_name))
    print(some_fixture_value)
© www.soinside.com 2019 - 2024. All rights reserved.