如何有条件地跳过参数化的 pytest 场景?

How can I conditionally skip a parameterized pytest scenario?

我需要标记要跳过的某些测试。但是,一些测试是参数化的,我只需要能够跳过某些场景。

我根据需要使用 py.test -m "hermes_only"py.test -m "not hermes_only" 调用测试。

简单测试用例使用以下标记:

@pytest.mark.hermes_only
def test_blah_with_hermes(self):

但是,我有一些参数化测试:

outfile_scenarios = [('buildHermes'),
                     ('buildTrinity')]

@pytest.mark.parametrize('prefix', outfile_scenarios)
def test_blah_build(self, prefix):
    self._activator(prefix=prefix)

我想要一种机制来过滤场景列表,或者如果定义了 pytest 标记则跳过某些测试。

更一般地说,我如何测试 pytest 标记的定义?

谢谢。

找到了!它的简洁优雅。我只是标记受影响的场景:

outfile_scenarios = [pytest.mark.hermes_only('buildHermes'),
                     ('buildTrinity')]

我希望这对其他人有帮助。

一个不错的解决方案from the documentation是这样的:

import pytest

@pytest.mark.parametrize(
    ("n", "expected"),
    [   
        (1, 2), 
        pytest.param(1, 0, marks=pytest.mark.xfail),
        pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")),
        (2, 3), 
        (3, 4), 
        (4, 5), 
        pytest.param(
            10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k")
        ),  
    ],  
)
def test_increment(n, expected):
    assert n + 1 == expected