测试框架pytest教程(9)跳过测试skip和xfail

skip无条件跳过

使用装饰器

python 复制代码
@pytest.mark.skip(reason="no way of currently testing this")
def test_example(faker):
    print("nihao")
    print(faker.words())

方法内部调用

满足条件时跳过

python 复制代码
def test_example():
    a=1
    if a>0:
        pytest.skip("unsupported configuration")

skipif满足条件跳过

python 复制代码
import sys


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

xfail预期失败

装饰器用法

python 复制代码
@pytest.mark.xfail
def test_function():
    ...

内部调用

python 复制代码
def test_function():
    if not valid_config():
        pytest.xfail("failing configuration (but should work)")

参数化时使用skip和xfail

python 复制代码
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
相关推荐
八了个戒4 分钟前
「数据可视化 D3系列」入门第三章:深入理解 Update-Enter-Exit 模式
开发语言·前端·javascript·数据可视化
失去妙妙屋的米奇6 分钟前
matplotlib数据展示
开发语言·图像处理·python·计算机视觉·matplotlib
夏天的阳光吖7 分钟前
C++蓝桥杯实训篇(四)
开发语言·c++·蓝桥杯
搞不懂语言的程序员32 分钟前
备忘录模式深度解析与实战案例
数据库·python·备忘录模式
angushine1 小时前
Gateway获取下游最终响应码
java·开发语言·gateway
爱的叹息1 小时前
关于 JDK 中的 jce.jar 的详解,以及与之功能类似的主流加解密工具的详细对比分析
java·python·jar
Lhuu(重开版1 小时前
2025第十六届蓝桥杯PythonB组部分题解
python
西贝爱学习1 小时前
数据结构:C语言版严蔚敏和解析介绍,附pdf
c语言·开发语言·数据结构
程丞Q香1 小时前
python——学生管理系统
开发语言·python·pycharm
晓纪同学2 小时前
C++ Primer (第五版)-第十三章 拷贝控制
java·开发语言·c++