在测试执行期间将测试标记为已跳过

Mark test as skipped during test execution

我有一个测试依赖于名为 fixture 的夹具。这是它的大致代码:

def test_optional_cool_feature(fixture):
    if not fixture.supports_cool_feature():
        return
    assert cool_feature() == expected_result

这里fixture是一个参数化的夹具,它存在于N个变体中,声明为

@py.test.fixture(scope="session", params=["type1", ...])
def fixture(request):
   if request.param = "type1":
       return Type1()
   elif ...

但是,当 运行 针对不支持 cool_feature。不幸的是,这无法通过 @py.test.mark.skipif('not fixture.supports_cool_feature()') 实现,因为 fixture 在执行 skipif 时仍然是一个函数,而不是替代的测试参数。

回答我自己的问题,因为我错过了 skipdocumentation 的部分,其中说明如何完全按照我的意愿去做:

imperative skip from within a test or setup function

If for some reason you cannot declare skip-conditions you can also imperatively produce a skip-outcome from within test or setup code. Example:

def test_function():
    if not valid_config():
        pytest.skip("unsupported configuration")