【问题标题】:Mark test as skipped during test execution在测试执行期间将测试标记为已跳过
【发布时间】:2015-03-17 19:34:09
【问题描述】:

我有一个依赖于名为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 输出中看到测试被跳过(而不是PASSED)也很好。不幸的是,@py.test.mark.skipif('not fixture.supports_cool_feature()') 无法实现这一点,因为fixture 在执行skipif 时仍然是一个函数,而不是替代的测试参数。

【问题讨论】:

    标签: testing pytest


    【解决方案1】:

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

    从测试或设置函数中强制跳过

    如果由于某种原因您不能声明跳过条件,您也可以 从测试或设置代码中强制生成跳过结果。 示例:

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

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      相关资源
      最近更新 更多