py.test跳过,具体取决于参数



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

@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

尽管其中一些组合没有意义。是否有一种简单的方法可以跳过这些组合,例如测试,例如这样的逻辑也有这样的逻辑:

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

好吧,这很容易:

@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

尽管在测试中运行'if'条件是有效的,但更正确的解决方案是指出"组合"有望在Xfail上失败。请参阅https://github.com/pytest-dev/pytest/discussions/8304

但是,目前不受Pytest的支持。

最新更新