测试框架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
相关推荐
m0_748245347 分钟前
python——Django 框架
开发语言·python·django
曼巴UE511 分钟前
UE5.3 C++ TArray系列(一)
开发语言·c++·ue5
熬夜苦读学习23 分钟前
Linux文件系统
linux·运维·服务器·开发语言·后端
菜鸟一枚在这31 分钟前
深度解析建造者模式:复杂对象构建的优雅之道
java·开发语言·算法
java1234_小锋34 分钟前
一周学会Flask3 Python Web开发-客户端状态信息Cookie以及加密
前端·python·flask·flask3
阿巴~阿巴~1 小时前
多源 BFS 算法详解:从原理到实现,高效解决多源最短路问题
开发语言·数据结构·c++·算法·宽度优先
B站计算机毕业设计超人1 小时前
计算机毕业设计Python+DeepSeek-R1高考推荐系统 高考分数线预测 大数据毕设(源码+LW文档+PPT+讲解)
大数据·python·机器学习·网络爬虫·课程设计·数据可视化·推荐算法
winfredzhang2 小时前
Python实战:Excel中文转拼音工具开发教程
python·安全·excel·汉字·pinyin·缩写
奔跑吧邓邓子2 小时前
【Python爬虫(34)】Python多进程编程:开启高效并行世界的钥匙
开发语言·爬虫·python·多进程
Heris993 小时前
2.22 c++练习【operator运算符重载、封装消息队列、封装信号灯集】
开发语言·c++