我想使用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
但我没有成功 .
1 回答
也许它对某些人有用 . 我找到了一种方法(灵感来自holgerkrekel.net/2009/05/13/parametrizing-python-tests-generalized/):