测试框架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
相关推荐
草莓熊Lotso5 分钟前
《算法闯关指南:优选算法--二分查找》--23.寻找旋转排序数组中的最小值,24.点名
开发语言·c++·算法·1024程序员节
foundbug99910 分钟前
C# 实现 Modbus TCP 通信
开发语言·tcp/ip·c#
郝学胜-神的一滴20 分钟前
主成分分析(PCA)在计算机图形学中的深入解析与应用
开发语言·人工智能·算法·机器学习·1024程序员节
離離原上譜32 分钟前
python-docx 安装与快速入门
python·word·python-docx·自动化办公·1024程序员节
JuicyActiveGilbert35 分钟前
【Python进阶】第2篇:单元测试
开发语言·windows·python·单元测试
今天没ID44 分钟前
Numpy加权平均数
python
唐叔在学习1 小时前
200kb能作甚?mss表示我给你整个截图程序
后端·python
智能化咨询1 小时前
Python 小工具实战:图片水印批量添加工具——从原理到实现的全流程指南
python
用户3721574261351 小时前
如何使用 Python 自动调整 Excel 行高和列宽
python
用户8356290780511 小时前
用Python自动化转换PowerPoint幻灯片为图片
后端·python