Pytest精通指南(18)多种手段过滤或升级警告

46 篇文章 1 订阅
32 篇文章 2 订阅


请添加图片描述

前言

pytest 中执行测试时,可能会出现警告,这些警告通常是由于代码中存在某些可能导致问题或不符合最佳实践的情况。

警告可能涉及各种方面,如未使用的变量、未实现的函数、过时的库用法等。

使用命令行实现过滤

模拟警告的代码示例

import warnings


def test_case_01():
    print("进入test_case_01函数")
    # 手动抛出一个警告
    warnings.warn(UserWarning("手动抛出一个警告"))
    assert 1 == 1

未处理警告,执行结果

执行命令pytest testcase/test_case_01.py -s -v

请添加图片描述

升级警告

执行命令pytest testcase/test_case_01.py -s -v -W error::UserWarning

请添加图片描述

忽略警告

执行命令pytest testcase/test_case_01.py -s -v -W ignore::UserWarning

请添加图片描述

忽略警告摘要

执行命令pytest testcase/test_case_01.py -s -v --disable-warnings

请添加图片描述

使用装饰器实现过滤

pytest 中,@pytest.mark.filterwarnings 装饰器允许我们向特定的测试项添加警告筛选器,以实现对警告的更细粒度控制。

通常,Pythonwarnings 模块用于控制警告的输出,而 pytest 通过其插件系统集成了对警告的管理。@pytest.mark.filterwarnings 装饰器提供了一种方便的方式来为特定的测试函数或方法定义警告筛选器。

我们可以使用 @pytest.mark.filterwarnings 来指定:

  • 要匹配的警告类别。
  • 警告消息的匹配模式。
  • 如何处理匹配的警告(例如,忽略它们或将其转换为错误)。
装饰方法

示例代码

import warnings

import pytest


def test_case_01():
    print("进入test_case_01函数")
    warnings.warn(UserWarning("手动抛出一个警告信息"))
    assert 1 == 1


# 忽略指定类型的警告信息
# @pytest.mark.filterwarnings("ignore::UserWarning")
# 忽略匹配正则表达式的警告信息
@pytest.mark.filterwarnings("ignore:.*模拟.*")
def test_case_02():
    print("进入test_case_02函数")
    warnings.warn(UserWarning("模拟程序中出现的警告信息"))
    assert 1 == 1

执行结果

请添加图片描述

装饰类

示例代码:忽略含有自定义的警告

import warnings

import pytest


@pytest.mark.filterwarnings("ignore:.*自定义.*")
class TestClassDemo2:
    print("进入类的内部")

    def test_case_01(self):
        print("进入test_case_01函数")
        warnings.warn(UserWarning("意外警告"))
        assert 1 == 1

    def test_case_02(self):
        print("进入test_case_02函数")
        warnings.warn(UserWarning("自定义警告"))
        assert 1 == 1

    def test_case_03(self):
        print("进入test_case_03函数")
        assert 1 == 1

执行结果

请添加图片描述

装饰模块

示例代码

import warnings

import pytest

# 不指定需要忽略的警告类型,表示忽略全部警告
# pytestmark = pytest.mark.filterwarnings("ignore")
# 忽略匹配正则表达式的警告信息
pytestmark = pytest.mark.filterwarnings("ignore:.*自定义.*")


def test_case():
    print("进入test_case函数")
    warnings.warn(UserWarning("自定义警告"))
    assert 1 == 1


class TestClassDemo2:
    print("进入类的内部")

    def test_case_01(self):
        print("进入test_case_01函数")
        warnings.warn(UserWarning("意外警告"))
        assert 1 == 1

    def test_case_02(self):
        print("进入test_case_02函数")
        warnings.warn(UserWarning("自定义警告"))
        assert 1 == 1

    def test_case_03(self):
        print("进入test_case_03函数")
        assert 1 == 1

执行结果

请添加图片描述

使用配置文件实现过滤

pytest.ini配置文件代码

[pytest]
filterwarnings =
    ignore
    error::UserWarning

示例代码

import warnings


def test_case():
    print("进入test_case函数")
    assert 1 == 1


class TestClassDemo2:
    print("进入类的内部")

    def test_case_01(self):
        print("进入test_case_01函数")
        warnings.warn(ImportWarning("模拟导入过时模块警告"))
        assert 1 == 1

    def test_case_02(self):
        print("进入test_case_02函数")
        warnings.warn(RuntimeWarning("模拟运行时警告"))
        assert 1 == 1

    def test_case_03(self):
        print("进入test_case_03函数")
        warnings.warn(SyntaxWarning("模拟语法警告"))
        assert 1 == 1

    def test_case_04(self):
        print("进入test_case_04函数")
        warnings.warn(UserWarning("自定义警告"))
        assert 1 == 1

执行结果

请添加图片描述

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

需要休息的KK.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值