如何取消涉及参数化的条件?

Swi*_*Run 5 pytest

如何取消涉及参数化的条件?

问题是这@pytest.mark.xfail(condition=(code == 123), reason="123 is an exception case")不起作用,因为代码是参数化变量。我尝试了一些不同的东西,包括静态类成员、全局变量以及将其设置在expected_setup夹具中。这些都不起作用(如预期)

@pytest.mark.xfail(condition=(code == 123), reason="123 is an exception case")
E   NameError: name 'code' is not defined
Run Code Online (Sandbox Code Playgroud)
params = [
    cls1,
    cls2,
    cls3,
    cls4
]
@pytest.mark.parametrize('code', params, ids=list(map(str, params)))
class TestContextExit(object):
    @pytest.fixture(autouse=True)
    def expected_setup(self, code):
        self.str1 = 'JETFUEL'
        self.str2 = 'OPERATIONNORTHWOODS'
        self.setup = NewStore(self.str1, self.str2, code)

    def test1(self, code):
        assert self.root.results.result_code == expected_result_code
        assert self.root.results.ta['result_code'] == expected_result_code.code
        assert self.root.results.result_code == expected_result_code

    @pytest.mark.xfail(condition=(code == 123), reason="123 is an exception case")
    def test2(self, code):
        assert self.setup.root['code'] == code

    @pytest.mark.xfail(condition=(code == 123), reason="123 is an exception case")
    def test3(self, code):
        assert self.setup.root['setup'] == NOERROR

Run Code Online (Sandbox Code Playgroud)

有什么想法或模式吗?查看 xfail pytest 文档,除了作为参数的一部分之外,我没有看到任何 xfail 关闭参数化的示例。但在这种情况下,类被参数化,并且只有两个类测试变成 xfails,而不是所有测试。

Sil*_*Guy 2

您可以使用 pytest.param 和 xfail 标记作为参数化中的参数值来 xfail 参数。

@pytest.mark.parametrize(code, [1,pytest.param(0,marks=pytest.mark.xfail(reason="reasons")]
def test_a():
    assert code
Run Code Online (Sandbox Code Playgroud)

这用于标记某些参数化值。这将使第二次测试失败。