Python py.test:从配置中获取不同夹具的测试功能的预期值

Python py.test:从配置中获取不同夹具的测试功能的预期值,python,pytest,Python,Pytest,我想使用py.test为项目中的不同级别设置测试,使用两个(或更多)装置。 对于每个夹具,我希望执行不同的测试功能,根据测试所用的夹具,通过参数获得预期值 这就是我想做的: def getGroups(boardstr, xlen, ylen): board = Board(boardstr, xlen, ylen) groups = MyClass.findGroups(board.get_field()) return groups @pytest.fixture(

我想使用py.test为项目中的不同级别设置测试,使用两个(或更多)装置。 对于每个夹具,我希望执行不同的测试功能,根据测试所用的夹具,通过参数获得预期值

这就是我想做的:

def getGroups(boardstr, xlen, ylen):
    board = Board(boardstr, xlen, ylen)
    groups = MyClass.findGroups(board.get_field())
    return groups

@pytest.fixture(scope='module')
def groups_3():
    # ... setup for level 3
    return getGroups(boardstr, xlen, ylen)

@pytest.fixture(scope='module')
def groups_10():
    # ... setup for level 10
    return getGroups(boardstr, xlen, ylen)

# this is my test data, which i want to use
expected_values = {
    "groups_3": {
        "test_total_groups": 9,
        "test_total_clickable_groups": 5,
        "test_total_colors": 3
    },
    "groups_10": {
        "test_total_groups": 22,
        "test_total_clickable_groups": 7,
        "test_total_colors": 3
    },
}

# "groups" shall be the fixture for the following test functions
# and the test methods shall be executed with groups_3 and groups_10 as fixture
def test_total_groups(groups, expected):
    assert len(groups) == expected

def test_total_clickable_groups(groups, expected):
    assert len([grp for grp in groups if grp.clickable is True]) == expected

def test_total_colors(groups, expected):
    assert len(np.unique([grp.color for grp in groups])) == expected
有没有办法通过py.test实现这一点?使用参数化功能是否可以实现这一点

我尝试了一些变体,如:

@pytest.mark.parametrize("groups, expected", [
        (groups_3(), 5),
        (groups_10(), 7),
    ])
def test_total_clickable_groups(groups, expected):
    assert len([grp for grp in groups if grp.clickable is True]) == expected

但是我没有成功。

也许它对某人有用。我找到了一种方法(灵感来源于):


你试过参数化测试吗?是的,但是我没有在同一个步骤中对两个不同的装置进行参数化。这可能吗?我不这么认为。我相信我在py.test中看到了这个行为的公开问题。
def getGroups(boardstr, xlen, ylen):
    board = Board(boardstr, xlen, ylen)
    groups = MyClass.findGroups(board.get_field())
    return groups

def groups_3():
    # ... setup for level 3
    return getGroups(boardstr, xlen, ylen)

def groups_10():
    # ... setup for level 10
    return getGroups(boardstr, xlen, ylen)

# decorator function
def params(funcarglist):
    def wrapper(function):
        function.funcarglist = funcarglist
        return function
    return wrapper

def pytest_generate_tests(metafunc):
    # called once per each test function
    for funcargs in getattr(metafunc.function, 'funcarglist', ()):
        # schedule a new test function run with applied **funcargs
        metafunc.addcall(funcargs=funcargs)

class TestClass:

    groups_3 = groups_3()
    groups_10 = groups_10()

    @params([dict(g=groups_3, e=9),
             dict(g=groups_10, e=22)])
    def test_total_groups(self, g, e):
        assert len(g) == e


    @params([dict(g=groups_3, e=5),
             dict(g=groups_10, e=7)])
    def test_total_clickable_groups(self, g, e):
        assert len([grp for grp in g if grp.clickable is True]) == e


    @params([dict(g=groups_3, e=3),
             dict(g=groups_10, e=3)])
    def test_total_colors(self, g, e):
        assert len(np.unique([grp.color for grp in g])) == e