测试框架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
相关推荐
xiaoqi019518 分钟前
机器学习因子分析实战:从量化特征到可视化决策
人工智能·python·机器学习
Escalating_xu29 分钟前
【Python】基础语法(1):常量、变量、类型、输入输出与运算符
开发语言·python
jimy11 小时前
polymorph.cpp里面的“析构函数、智能指针”问题
开发语言·c++
m0_380743871 小时前
PHP 8.3 缓存清除不生效怎么排查
开发语言·php
happylifetree1 小时前
Python14:核心语法-数据存储与运算-字符串定义
python
hhb_6181 小时前
AIRAGDebug:一键定位RAG链路异常
人工智能·python·算法
朝朝辞暮i1 小时前
C++ 第 6 课:switch —— 多状态选择
开发语言·c++·算法
小静AI工程实验室1 小时前
asyncio.Queue 有界队列实战:背压、task_done、join 与异常收尾
python
小凡geo1 小时前
本地商家 GEO:用脚本一键生成 FAQPage 结构化数据,让 AI 切片更稳
开发语言·人工智能·python·microsoft·搜索引擎·ai
Zhou1411361 小时前
SpringBoot_03_Web开发
前端·spring boot·python