测试框架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
相关推荐
图亚Vanta34 分钟前
Python入门第一课:Python安装、VSCode/Pycharm配置
vscode·python·pycharm
睿思达DBA_WGX1 小时前
使用 python-docx 库操作 word 文档(2):在word文档中插入各种内容
python·word
white-persist1 小时前
XXE 注入漏洞全解析:从原理到实战
开发语言·前端·网络·安全·web安全·网络安全·信息可视化
kunge1v52 小时前
学习爬虫第五天:自动化爬虫
爬虫·python·自动化
人生导师yxc2 小时前
Java中Mock的写法
java·开发语言
m***记2 小时前
Python 自动化办公的 10 大脚本
windows·python·自动化
半路程序员2 小时前
Go语言学习(四)
开发语言·学习·golang
人间乄惊鸿客2 小时前
python - 第二天
python
江上月5132 小时前
django与vue3的对接流程详解(上)
后端·python·django
老歌老听老掉牙2 小时前
基于 PyQt5 实现刀具类型选择界面的设计与交互逻辑
python·qt·交互