测试框架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_730115113 分钟前
模板编程中的SFINAE技巧
开发语言·c++·算法
2401_874732537 分钟前
Python Web爬虫入门:使用Requests和BeautifulSoup
jvm·数据库·python
2401_8318249614 分钟前
高性能计算集群部署
开发语言·c++·算法
武超杰24 分钟前
Spring 纯注解配置全解析(进阶版)
java·开发语言
机器视觉知识推荐、就业指导26 分钟前
LVGL真能动摇Qt的地位吗?
开发语言·qt·系统架构
add45a28 分钟前
C++代码移植性设计
开发语言·c++·算法
平常心cyk28 分钟前
Python基础快速复习——集合和字典
开发语言·数据结构·python
阿钱真强道29 分钟前
34 Python 离群点检测:什么是离群点?为什么要做异常检测?
python·sklearn·异常检测·异常·离群点检测
AC赳赳老秦31 分钟前
OpenClaw关键词挖掘Agent配置(附SOP脚本,可直接复制使用)
java·大数据·开发语言·人工智能·python·pygame·openclaw
qq_4160187233 分钟前
数据分析与科学计算
jvm·数据库·python