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

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

首先,看如下测试脚本,即在两个测试函数中人为抛出两个告警。

import warnings

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

def test_demo2():
    print("in test_demo2 ...")
    warnings.warn(UserWarning("warning,used to demo..."))
    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, configfile: pytest.ini
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_demo1
  E:\demo\test_demo.py:5: SyntaxWarning: warning,used to test...
    warnings.warn(SyntaxWarning("warning,used to test..."))

test_demo.py::test_demo2
  E:\demo\test_demo.py:10: UserWarning: warning,used to demo...
    warnings.warn(UserWarning("warning,used to demo..."))

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

(demo-HCIhX0Hq) E:\demo>

不论通过命令行的方式还是装饰器的方式,其实都可以在pytest.ini配置文件中配置,比如如下创建pytest.ini,编写如下代码,即指定filterwarnings可将所有的告警忽略不显示。

[pytest]
filterwarnings =
    ignore

执行结果如下,即此时不再打印告警信息。

(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, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 2 items

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

==================== 2 passed in 0.02s ====================

(demo-HCIhX0Hq) E:\demo>

同样若指定filterwarnings为error,则可将所有的告警转换为报错。

[pytest]
filterwarnings =
    error
	执行结果如下,即此时显示有两个失败,失败的原因则为产生的告警。
(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, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 2 items

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

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

    def test_demo1():
        print("in test_demo1 ...")
>       warnings.warn(SyntaxWarning("warning,used to test..."))
E       SyntaxWarning: warning,used to test...

test_demo.py:5: SyntaxWarning
_______________________ test_demo2 ________________________

    def test_demo2():
        print("in test_demo2 ...")
>       warnings.warn(UserWarning("warning,used to demo..."))
E       UserWarning: warning,used to demo...

test_demo.py:10: UserWarning
================= short test summary info =================
FAILED test_demo.py::test_demo1 - SyntaxWarning: warning,used to test...
FAILED test_demo.py::test_demo2 - UserWarning: warning,used to demo...
==================== 2 failed in 0.08s ====================

(demo-HCIhX0Hq) E:\demo>

当然也是可以指定忽略特定的告警类型的,如下即为忽略不显示UserWarning类型的告警。

[pytest]
filterwarnings =
    ignore::UserWarning
	执行结果如下,可以看出此时UserWarning类型的告警不再显示。
(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, configfile: pytest.ini
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_demo1
  E:\demo\test_demo.py:5: 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>

同样也是可以通过配置正则表达式来过滤告警,对匹配的告警进行忽略不显示处理或报错处理,如下即为对告警信息含有demo的的告警进行忽略不显示。

[pytest]
filterwarnings =
    ignore:.*demo.*

执行结果如下,此时含有demo的告警不再显示。

(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, configfile: pytest.ini
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_demo1
  E:\demo\test_demo.py:5: 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>

对于命令行中使用的参数,可以在pytest.ini中通过addopts指定,如下即关闭告警显示。

[pytest]
addopts=--disable-warnings
	执行结果如下,即显示有两条告警,但告警的具体内容不再显示。
(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, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 2 items

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

============== 2 passed, 2 warnings in 0.02s ==============

(demo-HCIhX0Hq) E:\demo>

通过指定如下参数,则可以彻底关闭告警。

[pytest]
addopts=-p no:warnings
	执行结果如下,即不再显示搞定内容。
(demo-HCIhX0Hq) E:\demo>pytest
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 2 items

test_demo.py ..                                      [100%]

==================== 2 passed in 0.02s ====================

(demo-HCIhX0Hq) E:\demo>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

redrose2100

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

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

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

打赏作者

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

抵扣说明:

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

余额充值