Pytest----如何通过filterwarnings配置不显示告警或将告警报错

【原文链接】Pytest----如何通过filterwarnings配置不显示告警或将告警报错

首先在脚本中人工抛出一条告警,测试代码如下所示:

import pytest
import warnings

def test_demo1():
    print("in test_demo1 ...")
    warnings.warn(SyntaxWarning("warning,used to test..."))
    assert 1==1

执行结果如下所示,即这里在执行结果中也显示了一条告警信息。

(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item

test_demo.py in test_demo1 ...
.

==================== warnings summary =====================
test_demo.py::test_demo1
  E:\demo\test_demo.py:7: SyntaxWarning: warning,used to test...
    warnings.warn(SyntaxWarning("warning,used to test..."))

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============== 1 passed, 1 warning in 0.02s ===============

(demo-HCIhX0Hq) E:\demo>

在脚本中可以通过filterwarnings来配置告警信息的处理方式,比如将其忽略不显示,则代码如下,即此时在测试函数中加一个装饰器装饰,在装饰器中使用ignore,注意当按照告警类型忽略不显示的时候,ignore后面是两个冒号。

import pytest
import warnings

@pytest.mark.filterwarnings("ignore::SyntaxWarning")
def test_demo1():
    print("in test_demo1 ...")
    warnings.warn(SyntaxWarning("warning,used to test..."))
    assert 1==1

执行结果如下,即不再有告警信息。

(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item

test_demo.py in test_demo1 ...
.

==================== 1 passed in 0.02s ====================

(demo-HCIhX0Hq) E:\demo>

同理,若将告警转换为报错,则只需要将代码中的装饰器中的ignore修改为error即可,如下:

import pytest
import warnings

@pytest.mark.filterwarnings("error::SyntaxWarning")
def test_demo1():
    print("in test_demo1 ...")
    warnings.warn(SyntaxWarning("warning,used to test..."))
    assert 1==1

执行结果如下,可以看到此时用例失败了,原因就是这个告警。

(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item

test_demo.py in test_demo1 ...
F

======================== FAILURES =========================
_______________________ test_demo1 ________________________

    @pytest.mark.filterwarnings("error::SyntaxWarning")
    def test_demo1():
        print("in test_demo1 ...")
>       warnings.warn(SyntaxWarning("warning,used to test..."))
E       SyntaxWarning: warning,used to test...

test_demo.py:7: SyntaxWarning
================= short test summary info =================
FAILED test_demo.py::test_demo1 - SyntaxWarning: warning,used to test...
==================== 1 failed in 0.07s ====================

(demo-HCIhX0Hq) E:\demo>

除此以外,还可以通过对告警信息进行正则匹配,比如当使用ignore时,此时ignore后面只跟一个冒号,冒号后面的即对告警信息的正则表达式,比如如下代码,即test_demo1中匹配告警信息中有test字符的就忽略,而test_demo2中则匹配告警信息中有demo字符的忽略,从代码分析,显然test_demo2中的告警信息无法匹配,即按照理论分析,test_demo1中的告警会忽略不显示,而test_demo2中的告警则会显示。

import pytest
import warnings

@pytest.mark.filterwarnings("ignore:.*test.*")
def test_demo1():
    print("in test_demo1 ...")
    warnings.warn(SyntaxWarning("warning,used to test..."))
    assert 1==1
    
@pytest.mark.filterwarnings("ignore:.*demo.*")
def test_demo2():
    print("in test_demo2 ...")
    warnings.warn(SyntaxWarning("warning,used to test..."))
    assert 1==1

执行结果如下,执行结果确实如此,即test_demo2中的告警继续报出来了,而test_demo1中的告警并未显示。

(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo
plugins: assume-2.4.3, rerunfailures-10.2
collected 2 items

test_demo.py in test_demo1 ...
.in test_demo2 ...
.

==================== warnings summary =====================
test_demo.py::test_demo2
  E:\demo\test_demo.py:13: SyntaxWarning: warning,used to test...
    warnings.warn(SyntaxWarning("warning,used to test..."))

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============== 2 passed, 1 warning in 0.02s ===============

(demo-HCIhX0Hq) E:\demo>

同理,将告警转换为报错信息,也是同样支持正则表达式的,比如如下代码:

import pytest
import warnings

@pytest.mark.filterwarnings("error:.*test.*")
def test_demo1():
    print("in test_demo1 ...")
    warnings.warn(SyntaxWarning("warning,used to test..."))
    assert 1==1

@pytest.mark.filterwarnings("error:.*demo.*")
def test_demo2():
    print("in test_demo2 ...")
    warnings.warn(SyntaxWarning("warning,used to test..."))
    assert 1==1

执行结果如下,显然test_demo1中的告警与正则表达匹配,因此报错了,而test_demo2中的告警并未匹配到正则表达式,因此不会报错,所以继续报出了告警信息。

(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo
plugins: assume-2.4.3, rerunfailures-10.2
collected 2 items

test_demo.py in test_demo1 ...
Fin test_demo2 ...
.

======================== FAILURES =========================
_______________________ test_demo1 ________________________

    @pytest.mark.filterwarnings("error:.*test.*")
    def test_demo1():
        print("in test_demo1 ...")
>       warnings.warn(SyntaxWarning("warning,used to test..."))
E       SyntaxWarning: warning,used to test...

test_demo.py:7: SyntaxWarning
==================== warnings summary =====================
test_demo.py::test_demo2
  E:\demo\test_demo.py:13: SyntaxWarning: warning,used to test...
    warnings.warn(SyntaxWarning("warning,used to test..."))

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================= short test summary info =================
FAILED test_demo.py::test_demo1 - SyntaxWarning: warning,used to test...
========= 1 failed, 1 passed, 1 warning in 0.07s ==========

(demo-HCIhX0Hq) E:\demo>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

redrose2100

您的鼓励是我最大的创作动力

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

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

打赏作者

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

抵扣说明:

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

余额充值