pytest.mark.skip 跳过测试用例

介绍

您可以标记无法在某些平台上运行或您预计会失败的测试功能,以便 pytest 可以相应地处理它们并提供测试会话的摘要,同时保持测试套件绿色。

跳过意味着您希望测试仅在满足某些条件时才能通过,否则 pytest 应该完全跳过运行测试。常见的例子是跳过非 Windows 平台上的纯 Windows 测试,
或者跳过依赖于目前不可用的外部资源(例如数据库)的测试。

xfail意味着您希望测试由于某种原因而失败。一个常见的例子是测试尚未实现的功能或尚未修复的错误。当一个测试通过但预期会失败(标记为pytest.mark.xfail)时,
它是一个xpass并且将在测试摘要中报告

通过标记的方式跳过测试用例

```
@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
```

在用例内通过判断强制跳过

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

跳过整个模块:

import sys
import pytest

if not sys.platform.startswith("win"):
    pytest.skip("skipping windows-only tests", allow_module_level=True)

执行打印额外现实跳过的信息

  • pytest分别计算和列出skip和xfail测试。默认情况下不显示有关已跳过/xfailed 测试的详细信息,以避免使输出混乱。您可以使用该-r选项查看与测试进度中显示的“短”字母相对应的详细信息:
    pytest -rxXs  # show extra info on xfailed, xpassed, and skipped tests
    

官网关于skip的教程

posted @ 2022-06-29 15:22  zhq9  阅读(75)  评论(0编辑  收藏  举报