似乎py.test在评估的表达式时不使用测试装置
skipif。以您的示例为例,
test_ios它实际上是成功的,因为它会将模块名称空间中找到的
函数
platform与
"ios"字符串进行比较,该字符串求值
False后将执行测试并成功。如果pytest如您期望的那样插入夹具进行评估,则应该跳过该测试。
解决您的问题(但不是解决您的问题)的方法是实施一种夹具,以检查测试中的标记,并相应地跳过它们:
# conftest.pyimport pytest@pytest.fixturedef platform(): return "ios"@pytest.fixture(autouse=True)def skip_by_platform(request, platform): if request.node.get_closest_marker('skip_platform'): if request.node.get_closest_marker('skip_platform').args[0] == platform: pytest.skip('skipped on this platform: {}'.format(platform))
关键是
autouse参数,它将使该夹具被所有测试自动包括在内。然后,您的测试可以像这样标记要跳过的平台:
@pytest.mark.skip_platform('ios')def test_ios(platform, request): assert 0, 'should be skipped'
希望有帮助!