4.pytest跳过测试用例

跳过测试用例方法

一. norecursedirs [不推荐]

在递归收集测试用例时,可以避免收集某些目录

在pytest.ini修改
[pytest]
norecursedirs = .svn _build tmp*


默认模式为 '*.egg''.*''_darcs''build''CVS''dist''node_modules''venv''{{arch}}' . 设置一个 norecursedirs 替换默认值。

此外, pytest 将尝试通过激活脚本智能地识别和忽略virtualenv。任何被视为虚拟环境根目录的目录在测试收集期间都不会被考虑,除非 ‑‑collect‑in‑virtualenv 给出。

还要注意 norecursedirs 优先于 ‑‑collect‑in‑virtualenv ;例如,如果要在具有匹配的基目录的virtualenv中运行测试 '.*' 你 must 覆盖 norecursedirs 除了使用 ‑‑collect‑in‑virtualenv 旗帜。

二. 命令执行参数

--ignore=path【允许有多个参数】

--ignore-glob='*_01.py'

--deselect=路径::test函数【允许有多个参数】

三. skip

跳过测试函数的最简单方法是用 skip 可以传递可选 reason

@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 sys
import pytest

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

四. skipif

如果您希望有条件地跳过某些内容,则可以使用 skipif 相反。以下是在python3.6之前的解释器上运行时将测试函数标记为跳过的示例:

import sys

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

跳过类或模块的所有测试函数

你可以使用 skipif 类上的标记(作为任何其他标记):

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

如果条件是 True ,此标记将为该类的每个测试方法生成跳过结果。

如果要跳过模块的所有测试功能,可以使用 pytestmark 全球的:

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

五. xfail:将测试功能标记为预期失败

基本使用

你可以使用 xfail 标记以指示预期测试失败:

@pytest.mark.xfail
def test_function():
    ...

此测试将运行,但失败时不会报告回溯。相反,终端报告会将其列在“预期失败”中 (XFAIL)或“意外通过” (XPASS)

或者,也可以将测试标记为 XFAIL 必须从测试或其设置功能中:

def test_function():
    if not valid_config():
        pytest.xfail("failing configuration (but should work)")
def test_function2():
    import slow_module
    if slow_module.slow_function():
        pytest.xfail("slow_module taking too long")

这两个示例说明了您不希望在模块级别检查条件的情况,也就是说,在这种情况下,条件将被评估为标记。

这将使 test_function XFAIL . 注意,在 pytest.xfail() 呼叫,与标记不同。这是因为它是通过引发一个已知的异常在内部实现的。

参考文献pytest.mark.xfail

condition 参数

如果测试只在特定条件下失败,则可以将该条件作为第一个参数通过:

@pytest.mark.xfail(sys.platform == "win32", reason="bug in a 3rd party library")
def test_function():
    ...

请注意,您还必须传递一个原因(请参阅 pytest.mark.xfail

reason 参数

可以使用 reason 参数:

@pytest.mark.xfail(reason="known parser issue")
def test_function():
    ...

raises 参数

如果要更具体地说明测试失败的原因,可以在 raises 参数。

@pytest.mark.xfail(raises=RuntimeError)
def test_function():
    ...

如果测试失败,则报告为常规失败,除非 raises .

run 参数

如果测试应标记为xfail并报告为xfail,但不应执行,则使用 run 参数AS False

@pytest.mark.xfail(run=False)
def test_function():
    ...

这对于Xfailing测试特别有用,这些测试会使解释器崩溃,应该稍后进行调查。

strict 参数

两个 XFAILXPASS 默认情况下不要让测试套件失败。您可以通过设置 strict 仅关键字参数到 True

@pytest.mark.xfail(strict=True)
def test_function():
    ...

这将使 XPASS 此测试的(“意外通过”)结果将使测试套件失败。

您可以更改 strict 参数使用 xfail_strict ini选项:

[pytest]
xfail_strict=true

xfail也执行

通过在命令行上指定:

pytest --runxfail

您可以强制运行和报告 xfail 将测试标记为完全没有标记。这也导致 pytest.xfail() 不产生效果。

实例

下面是一个简单的测试文件,有几种用法:

import pytest

xfail = pytest.mark.xfail

@xfail
def test_hello():
    assert 0

@xfail(run=False)
def test_hello2():
    assert 0

@xfail("hasattr(os, 'sep')")
def test_hello3():
    assert 0

@xfail(reason="bug 110")
def test_hello4():
    assert 0

@xfail('pytest.__version__[0] != "17"')
def test_hello5():
    assert 0

def test_hello6():
    pytest.xfail("reason")

@xfail(raises=IndexError)
def test_hello7():
    x = []
    x[1] = 1

使用“报告xfail”选项运行它可以得到以下输出:

example $ pytest -rx xfail_demo.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR/example
collected 7 items

xfail_demo.py xxxxxxx                                                [100%]

========================= short test summary info ==========================
XFAIL xfail_demo.py::test_hello
XFAIL xfail_demo.py::test_hello2
  reason: [NOTRUN]
XFAIL xfail_demo.py::test_hello3
  condition: hasattr(os, 'sep')
XFAIL xfail_demo.py::test_hello4
  bug 110
XFAIL xfail_demo.py::test_hello5
  condition: pytest.__version__[0] != "17"
XFAIL xfail_demo.py::test_hello6
  reason: reason
XFAIL xfail_demo.py::test_hello7
============================ 7 xfailed in 0.12s ============================

跳过/xfail,参数化

当使用参数化时,可以对单个测试实例应用诸如skip和xfail之类的标记:

import sys
import pytest


@pytest.mark.parametrize(
    ("n", "expected"),
    [
        (1, 2),
        pytest.param(1, 0, marks=pytest.mark.xfail),
        pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")),
        (2, 3),
        (3, 4),
        (4, 5),
        pytest.param(
            10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k")
        ),
    ],
)
def test_increment(n, expected):
    assert n + 1 == expected
posted @   winkie  阅读(443)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示