测试框架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
相关推荐
吴维炜8 分钟前
「Python算法」计费引擎系统SKILL.md
python·算法·agent·skill.md·vb coding
吕司25 分钟前
Qt的信号与槽
开发语言·qt
FansyMeng1 小时前
VSCode配置anaconda
vscode·python
电饭叔1 小时前
Tkinter Button 括号内的核心参数详解
python·学习
bjxiaxueliang1 小时前
一文掌握C/C++命名规范:风格、规则与实践详解
c语言·开发语言·c++
ktoking1 小时前
Stock Agent AI 模型的选股器实现 [五]
人工智能·python
地球资源数据云1 小时前
SCI制图——云雨图
python·信息可视化·数据分析
玄〤1 小时前
Java 大数据量输入输出优化方案详解:从 Scanner 到手写快读(含漫画解析)
java·开发语言·笔记·算法
独自破碎E1 小时前
Spring Boot + LangChain4j 报错:Bean 类型不匹配的解决办法
spring boot·python·pycharm
小W与影刀RPA2 小时前
【影刀 RPA】 :文档敏感词批量替换,省时省力又高效
人工智能·python·低代码·自动化·rpa·影刀rpa