zoukankan      html  css  js  c++  java
  • pytest八:skip 跳过用例

    这是一个快速指南,介绍如何在不同情况下跳过模块中的测试
    1.无条件地跳过模块中的所有测试:
    pytestmark = pytest.mark.skip("all tests still WIP")
    2.根据某些条件跳过模块中的所有测试
    pytestmark = pytest.mark.skipif(sys.platform == "win32",
    "tests for linux
    ˓→ only"
    3.如果缺少某些导入,则跳过模块中的所有测试
    pexpect = pytest.importorskip("pexpect")



    skip 跳过用例
    pytest.mark.skip 可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能
    skip 意味着叧有在满足某些条件时才希望测试通过,否则 pytest应该跳过运行测试。 常见示例是在非 Windows 平台上跳过仅限Windows 的测试,或跳过测试依赖于当前不可用的外部资源(例如数据库)。
    xfail 意味着您希望测试由于某种原因而失败。 一个常见的例子是对功能的测试尚未实施,或尚未修复的错误。 当测试通过时尽管预计会失败(标有 pytest.mark.xfail),它是一个 xpass,将在测试摘要中报告。
    pytest 计数并分别列出 skip 和 xfail 测试。 未显示有关跳过/
    xfailed 测试的详细信息默认情况下,以避免混乱输出。 您可以使用-r选顷查看不“short”字母对应的详细信息显示在测试进度中
    > pytest -rxXs # show extra info on xfailed, xpassed, and skipped tests
    有关-r 选项的更多详细信息,请运行 pytest -hskip
    跳过测试函数的最简单方法是使用跳过装饰器标记它,可以传递一个可选的原因
    @pytest.mark.skip(reason="no way of currently testing this")
    def test_the_unknown():
      ...
    或者,也可以通过调用来在测试执行或设置期间强制跳过pytest.skip(reason)功能:
    def test_function():
    if not valid_config():
    pytest.skip("unsupported configuration")
    也可以使用 pytest.skip(reason,allow_module_level = True)跳过整个模块级别:
    import pytest
    if not pytest.config.getoption("--custom-flag"):
    pytest.skip("--custom-flag is missing, skipping tests", allow_module_level=True)
    当在导入时间内无法评估跳过条件时,命令性方法很有用。


    skipif
    如果您希望有条件地跳过某些内容,则可以使用 skipif 代替。 这是标记测试的示例在 Python3.6 之前的解释器上运行时要跳过的函数

    import sys
    import pytest
    @pytest.mark.skipif(sys.version_info < (3,6),
    reason="requires python3.6 or higher")
    def test_function():
    ...
    如果条件在收集期间评估为 True,则将跳过测试函数,具有指定的原因使用-rs 时出现在摘要中。

    import mymodule
    minversion = pytest.mark.skipif(mymodule.__versioninfo__ <
    (1,1),
    reason="at least mymodule-1.1 required")
    @minversion
    def test_function():
    ...
    您可以在模块之间共享 skipif 标记。参考以下案例

    from test_mymodule import minversion
    @minversion
    def test_anotherfunction():
    ...

    您可以导入标记并在另一个测试模块中重复使用它:
    对于较大的测试套件,通常最好有一个文件来定义标记,然后一致适用于整个测试套件。
    或者,您可以使用条件字符串而不是布尔值,但它们之间不能轻易共享它们支持它们主要是出于向后兼容的原因



    skip 类或模块
    您可以在类上使用 skipif 标记(与任何其他标记一样):


    @pytest.mark.skipif(sys.platform == 'win32',
    reason="does not run on windows")
    class TestPosixCalls(object):
    def test_function(self):
    "will not be setup or run under 'win32' platform"

    如果条件为 True,则此标记将为该类的每个测试方法生成跳过结果
    如果要跳过模块的所有测试功能,可以在全局级别使用pytestmark名称

    pytestmark = pytest.mark.skipif(...)
    如果将多个 skipif 装饰器应用于测试函数,则如果任何跳过条件为真,则将跳过它


    skip 文件或目录
    有时您可能需要跳过整个文件或目录,例如,如果测试依赖于特定于 Python 的版本功能或包含您不希望 pytest 运行的代码。 在返种情况下,您必项排除文件和目录来自收藏。 有关更多信息,请参阅自定义测试集合。

    skip 缺少导入依赖项
    您可以在模块级别戒测试戒测试设置功能中使用以下帮助程序
    docutils = pytest.importorskip("docutils")
    如果无法在此处导入 docutils,则会导致测试跳过结果。 你也可以跳过库的版本号
    docutils = pytest.importorskip("docutils",
    minversion="0.3")
    将从指定模块的属性中读取版本。












  • 相关阅读:
    server.Execute 执行子请求时出错
    mybatis逆向工程
    上传及下载github项目
    基于tess4j的图片文字提取
    myeclipse中更改默认jdk版本出错( Target is not a JDK root. System library was not found)
    SSM整合环境搭建demo
    AMD CPU环境下使用android studio,eclipse的Genymotion插件
    Android Studio电脑不支持HAXM的解决办法
    完整使用JDBC访问数据库
    springMVC保存数据到mysql数据库中文乱码问题解决方法
  • 原文地址:https://www.cnblogs.com/zhongyehai/p/9681242.html
Copyright © 2011-2022 走看看