pytest学习总结2.13 - skip、xfail使用全集

2.13 skip和xfail处理不能成功的测试用例

  标记不能在某些平台上运行或您希望失败的测试函数,以便pytest可以相应地处理它们,并呈现测试会话的摘要,同时保持测试套件的绿色,跳过意味着您只有在满足某些条件时才期望测试通过,否则pytest应该完全跳过运行测试。

  常见的例子是跳过非windows平台上的仅windows测试
xfail意味着您期望测试由于某种原因而失败。还有一个常见的例子是对尚未实现的特性或尚未修复的错误的测试。

  当测试通过,尽管预期失败(用pytest.mark.xfail标记)时,它是xpass,将在测试摘要中报告。

2.13.1 跳过测试用例

1. 装饰器标记跳过,并传递跳过原因:

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

2. pytest.skip 强制性跳过

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

3. 参数化中跳过用例:

import pytest
@pytest.mark.parametrize("i", range(50))
def test_num(i):
    if i in (17, 25):
        pytest.skip("跳过用例")

4. 跳过平台是win的所有用例

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

5. 有条件的跳过用例

如果您希望有条件地跳过某些内容,下面是一个标记在Python3.10之前的解释器上运行时要跳过的测试函数的例子:

import sys
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher˓→")
def test_function():
    ...

6. 基于某些条件跳过某些测试用例

在模块(py文件)之间共享 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():
    pass
# test_myothermodule.py
from test_mymodule import minversion

@minversion
def test_anotherfunction():
    pass

7. 跳过一个类,在类上使用 skipif 标记

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

8. 跳过一个模块的所有测试函数,您可以使用全局测试标记

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

9. 跳过文件或目录:直接pytest后面跟着对应目录的用例就可以了

2.13.2 将测试用例标记为失败

1. 使用xfail标记来指示您希望测试失败:

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

2. 运行测试过程中设置失败

在pytest.xfail()调用之后没有执行其他代码。这是因为它是通过引发一个已知的异常而在内部实现的。

import pytest
@pytest.mark.parametrize("i", range(50))
def test_num(i):
    if i in (17, 25):
        pytest.xfail("预期失败")

3. 在某个条件、原因下失败

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

4. 禁止运行标记的用例

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

5. 设置strict使xfail的测试用例运行结果为失败

import pytest
@pytest.mark.xfail(strict=True)
def test_function():
    pass
[pytest]
xfail_strict=true

6. 相关代码示例

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
C:\Users\mc\Desktop\python基础>pytest -rx test_module.py
======================================================================= test session starts ========================================================================
platform win32 -- Python 3.9.6, pytest-7.1.1, pluggy-0.13.1
rootdir: C:\Users\mc\Desktop\python基础
collected 7 items                                               
test_module.py xxxxxxx                               [100%]

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

2.13.3 使用参数化跳过/退出

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
C:\Users\mc\Desktop\python>pytest -vs test_module.py
======================================================================= test session starts ========================================================================
platform win32 -- Python 3.9.6, pytest-7.1.1, pluggy-0.13.1 -- c:\python39\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\mc\Desktop\python
collected 7 items

test_module.py::test_increment[1-2] PASSED
test_module.py::test_increment[1-0] XFAIL
test_module.py::test_increment[1-3] XFAIL (some bug)
test_module.py::test_increment[2-3] PASSED
test_module.py::test_increment[3-4] PASSED
test_module.py::test_increment[4-5] PASSED
test_module.py::test_increment[10-11] SKIPPED (py2k˓→)
============================================================= 4 passed, 1 skipped, 2 xfailed in 0.12s ==============================================================
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿_焦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值