py.test跳过取决于参数

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

我希望通过使用所有可能的参数组合调用它来详尽地测试函数:

@pytest.mark.parametrize("a", [1, 2])
@pytest.mark.parametrize("b", [1, 2, 3, 4])
@pytest.mark.parametrize("c", [1, 2, 3])
@pytest.mark.parametrize("d", [1, 2])
def test_func_variations(a, b, c, d):
    assert func(a, b, c, d) == a*b*c+d

虽然其中一些组合没有意义。有没有一种简单的方法可以跳过py.test的这些组合,例如也有这样的逻辑:

def test_func_variations(a, b, c, d):
    if (a == 1 and b in (2, 3)) or (a == 2 and c == 3):
        skip_me()
    assert func(a, b, c, d) == a*b*c+d
python unit-testing pytest
1个回答
4
投票

好的,这很简单:

@pytest.mark.parametrize("a", [1, 2])
@pytest.mark.parametrize("b", [1, 2, 3, 4])
@pytest.mark.parametrize("c", [1, 2, 3])
@pytest.mark.parametrize("d", [1, 2])
def test_func_variations(a, b, c, d):
    if (a == 1 and b in (2, 3)) or (a == 2 and c == 3):
        pytest.skip("invalid parameter combination")
    assert func(a, b, c, d) == a*b*c+d
© www.soinside.com 2019 - 2024. All rights reserved.