Pytest----如何通过pytest.ini配置不显示告警或将告警报错
创始人
2024-03-16 17:16:27
0

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

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

import warningsdef test_demo1():print("in test_demo1 ...")warnings.warn(SyntaxWarning("warning,used to test..."))assert 1==1def 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 itemstest_demo.py in test_demo1 ...
.in test_demo2 ...
.==================== warnings summary =====================
test_demo.py::test_demo1E:\demo\test_demo.py:5: SyntaxWarning: warning,used to test...warnings.warn(SyntaxWarning("warning,used to test..."))test_demo.py::test_demo2E:\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 itemstest_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 itemstest_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 itemstest_demo.py in test_demo1 ...
.in test_demo2 ...
.==================== warnings summary =====================
test_demo.py::test_demo1E:\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 itemstest_demo.py in test_demo1 ...
.in test_demo2 ...
.==================== warnings summary =====================
test_demo.py::test_demo1E:\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 itemstest_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 itemstest_demo.py ..                                      [100%]==================== 2 passed in 0.02s ====================(demo-HCIhX0Hq) E:\demo>

相关内容

热门资讯

多城联动!辽宁消防“安全盛宴”...   5月8日,辽宁省消防救援总队以创意形式和贴心服务,在鞍山、抚顺、丹东、锦州、营口等地同步开展多元...
大美江湖,天下岳阳   千年巴陵,江湖旧梦;一座岳阳,四水归心。  当千年岳阳楼的飞檐在XR空间里再度挑起月色,当洞庭湖...
王帅红在全市开发区工作推进会上... 5月9日,全市开发区工作推进会召开。会议深入学习贯彻习近平总书记关于开发区工作的重要指示精神,认真贯...
林志玲深夜晒合照回忆与大S首次... 5月10日晚,林志玲晒合照,怀念第一次见到大S:第一次见到大S,是在一个冰淇淋店。当时还没有入行的我...
boc某行刚涨薪了? (转自:五道口江湖)近日,有多名山东、河北、河南地区boc行的网友分享,上个月涨薪了。其中山东地区的...
CPI环比由降转涨 部分工业行... 转自:衢州日报  4月份,全国居民消费价格指数(CPI)环比由上月下降0.4%转为上涨0.1%,扣除...
应急处置演练 转自:衢州日报  近日,柯城区衢化街道组织开展防汛防台应急处置演练。此次演练包括桌面推演和卫星电话、...
胡忠雄主持召开贵安新区党工委巡... 5月10日,省委常委、贵安新区党工委书记胡忠雄主持召开新区党工委巡视整改工作领导小组会议。他强调,要...
“夜游信安湖”入选省级精品航线 转自:衢州日报  本报讯 (通讯员 吴丽芬 戴晨涛) 近日,全省水路交旅融合品牌——“泛舟浙里”发布...
中原建业:1-4月合约销售35... 业绩快报5月9日,中原建业有限公司(股票代码:09982.HK,以下简称“中原建业”)发布2025年...
新华社评论员:守护历史记忆,共... 转自:北京日报客户端当地时间5月9日,俄罗斯莫斯科红场碧空澄澈、旗帜招展,纪念苏联伟大卫国战争胜利8...
2025年前4个月,我国货物贸... 海关总署日前发布数据显示,2025年前4个月,以人民币计,我国货物贸易进出口总值14.14万亿元,同...
南京鼓楼城管上门服务指导湖南路... 转自:扬子晚报扬子晚报网5月10日讯(通讯员 李亦军 记者 张可)近期,南京鼓楼城管大队湖管会中队联...
多出去玩能减少身体炎症 多接触大自然的人更健康与城市相比,自然环境中的景色、气味、声音更能促进身心健康。发表于美国《大脑行为...
头部投行美女转行瑜伽! (转自:五道口江湖)一名香港普拉提老师说:一年前的差不多这个時候我交还了Morgan Stanley...
中自科技携手天津大学、王成山院... 5月8日,在成都举办的“青城论道低碳能源共生发展”大会上,中自科技股份有限公司与天津大学、中国工程院...
鲁慕迅逝世,享年98岁 湖北省文联鲁慕迅同志治丧小组5月9日发布讣告,湖北省文联离休干部、省美术家协会原副主席、一级美术师鲁...
听·见|“被名言”借助网络泛滥... ——莫言说:“我不够成熟,不够圆滑,不够老练。没关系,我只不过是一个晚熟的人。”原来莫爷爷是我的同类...
曝光AL保险资管公司领导 (转自:五道口江湖)近日网传的一份文件,吐槽AL保险资管公司领导,对实习生不尊重,缺乏专业能力,人际...
成都蓉城客场4-0横扫梅州客家... 封面新闻记者 陈羽啸北京时间5月10日晚,2025赛季中超联赛第12轮在五华奥体中心展开争夺,成都蓉...