测试框架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
相关推荐
小呀小萝卜儿13 分钟前
2025-11-14 学习记录--Python-特征归一化方法(Min-Max或StandardScaler)
开发语言·python·学习
顾安r20 分钟前
11.14 脚本网页 青蛙过河
服务器·前端·python·游戏·html
测试199827 分钟前
如何用Appium实现移动端UI自动化测试?
自动化测试·软件测试·python·测试工具·ui·职场和发展·appium
雪域迷影33 分钟前
C++ 11 中的move赋值运算符
开发语言·c++·move
jf加菲猫41 分钟前
第2章 Hello World
开发语言·c++·qt·ui
Python私教1 小时前
第一个Python金融爬虫
爬虫·python·金融
todoitbo1 小时前
Rust新手第一课:Mac环境搭建踩坑记录
开发语言·macos·rust
laplace01232 小时前
PyQt5 + Qt Designer配置指令
开发语言·qt
nvd112 小时前
Python 迭代器 (Iterator) vs. 生成器 (Generator)
开发语言·python