Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python py.test跳过取决于参数_Python_Unit Testing_Pytest - Fatal编程技术网

Python py.test跳过取决于参数

Python py.test跳过取决于参数,python,unit-testing,pytest,Python,Unit Testing,Pytest,我希望通过使用所有可能的参数组合调用函数来彻底测试函数: @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,

我希望通过使用所有可能的参数组合调用函数来彻底测试函数:

@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
好吧,那很简单:

@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上预期失败的“组合”。看


但是,pytest目前不支持这一点。

我的意思是,您可以按照问题中显示的逻辑,但将
skip___me
替换为
pytest.skip
。您对@jornsharpe的回答有点遗漏,谢谢:)感谢您提供更多信息。然而,这实际上并不是问题的答案。(而且……这个问题已经有了一个似乎有效的答案。)很快,当你获得50的声誉时,你将能够发表评论;这将是一个分享这种想法的好方法。