pytest-12-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 -h

skip

跳过测试函数的最简单方法是使用跳过装饰器标记它,可以传递一个可选的原因

@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
@pytest.mark.skipif(sys.version_info < (3,6), reason="requires python3.6 or higher") def test_function(): ...

如果条件在收集期间评估为True,则将跳过测试函数,具有指定的原因使用-rs时出现在摘要中。

您可以在模块之间共享skipif标记。参考以下案例

# content of test_mymodule.py
import mymodule
minversion = pytest.mark.skipif(mymodule.__versioninfo__ < (1,1),
reason="at least mymodule-1.1 required") @minversion def test_function(): ...

您可以导入标记并在另一个测试模块中重复使用它:

# test_myothermodule.py
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,则此标记将为该类的每个测试方法生成跳过结果

警告:强烈建议不要在使用继承的类上使用skipif。 pytest中的一个已知错误标记可能会导致超类中的意外行为。

如果要跳过模块的所有测试功能,可以在全局级别使用pytestmark名称

# test_module.py
pytestmark = pytest.mark.skipif(...)

如果将多个skipif装饰器应用于测试函数,则如果任何跳过条件为真,则将跳过它

skip文件或目录

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

skip缺少导入依赖项

您可以在模块级别或测试或测试设置功能中使用以下帮助程序

docutils = pytest.importorskip("docutils")

如果无法在此处导入docutils,则会导致测试跳过结果。 你也可以跳过库的版本号

docutils = pytest.importorskip("docutils", minversion="0.3")

将从指定模块的__version__属性中读取版本。

概要

这是一个快速指南,介绍如何在不同情况下跳过模块中的测试

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")

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 -h

skip

跳过测试函数的最简单方法是使用跳过装饰器标记它,可以传递一个可选的原因

@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
@pytest.mark.skipif(sys.version_info < (3,6), reason="requires python3.6 or higher") def test_function(): ...

如果条件在收集期间评估为True,则将跳过测试函数,具有指定的原因使用-rs时出现在摘要中。

您可以在模块之间共享skipif标记。参考以下案例

# content of test_mymodule.py
import mymodule
minversion = pytest.mark.skipif(mymodule.__versioninfo__ < (1,1),
reason="at least mymodule-1.1 required") @minversion def test_function(): ...

您可以导入标记并在另一个测试模块中重复使用它:

# test_myothermodule.py
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,则此标记将为该类的每个测试方法生成跳过结果

警告:强烈建议不要在使用继承的类上使用skipif。 pytest中的一个已知错误标记可能会导致超类中的意外行为。

如果要跳过模块的所有测试功能,可以在全局级别使用pytestmark名称

# test_module.py
pytestmark = pytest.mark.skipif(...)

如果将多个skipif装饰器应用于测试函数,则如果任何跳过条件为真,则将跳过它

skip文件或目录

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

skip缺少导入依赖项

您可以在模块级别或测试或测试设置功能中使用以下帮助程序

docutils = pytest.importorskip("docutils")

如果无法在此处导入docutils,则会导致测试跳过结果。 你也可以跳过库的版本号

docutils = pytest.importorskip("docutils", minversion="0.3")

将从指定模块的__version__属性中读取版本。

概要

这是一个快速指南,介绍如何在不同情况下跳过模块中的测试

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")

猜你喜欢

转载自www.cnblogs.com/jason89/p/10323591.html