测试框架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
相关推荐
Mr...Gan几秒前
TypeScript
开发语言·javascript·typescript
星寂樱易李24 分钟前
软件开发和嵌入式开发岗位的面试题
python
前端 贾公子1 小时前
《Vuejs设计与实现》第 8 章(挂载与更新)
开发语言·前端·javascript
ღ 噫吁嚱1 小时前
【C/C++】实现固定地址函数调用
c语言·开发语言·c++
咔咔库奇2 小时前
axios取消请求
开发语言·前端·javascript
寒山李白2 小时前
Java中高并发线程池的相关面试题详解
java·开发语言·面试·高并发·线程池·多线程
幼儿园口算大王2 小时前
Java是实现大根堆
java·开发语言
0白露2 小时前
java的深拷贝与浅拷贝
java·开发语言
zh_199952 小时前
用纯.NET开发并制作一个智能桌面机器人(五):使用.NET为树莓派开发Wifi配网功能
开发语言·php·.net·树莓派
404.Not Found2 小时前
Day50 Python打卡训练营
python·深度学习·机器学习