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>

相关内容

热门资讯

Python|位运算|数组|动... 目录 1、只出现一次的数字(位运算,数组) 示例 选项代...
张岱的人物生平 张岱的人物生平张岱(414年-484年),字景山,吴郡吴县(今江苏苏州)人。南朝齐大臣。祖父张敞,东...
西游西后传演员女人物 西游西后传演员女人物西游西后传演员女人物 孙悟空 六小龄童 唐僧 徐少华 ...
名人故事中贾岛作诗内容简介 名人故事中贾岛作诗内容简介有一次,贾岛骑驴闯了官道.他正琢磨着一句诗,名叫《题李凝幽居》全诗如下:闲...
和男朋友一起优秀的文案? 和男朋友一起优秀的文案?1.希望是惟一所有的人都共同享有的好处;一无所有的人,仍拥有希望。2.生活,...
戴玉手镯的好处 戴玉手镯好还是... 戴玉手镯的好处 戴玉手镯好还是碧玺好 女人戴玉?戴玉好还是碧玺好点佩戴手镯,以和田玉手镯为佳!相嫌滑...
依然什么意思? 依然什么意思?依然(汉语词语)依然,汉语词汇。拼音:yī    rán基本解释:副词,指照往常、依旧...
高尔基的散文诗 高尔基的散文诗《海燕》、《大学》、《母亲》、《童年》这些都是比较出名的一些代表作。
心在飞扬作者简介 心在飞扬作者简介心在飞扬作者简介如下。根据相关公开资料查询,心在飞扬是一位优秀的小说作者,他的小说作...
卡什坦卡的故事赏析? 卡什坦卡的故事赏析?讲了一只小狗的故事, 我也是近来才读到这篇小说. 作家对动物的拟人描写真是惟妙...
林绍涛为简艾拿绿豆糕是哪一集 林绍涛为简艾拿绿豆糕是哪一集第三十二集。 贾宽认为是阎帅间接导致刘映霞住了院,第二天上班,他按捺不...
小爱同学是女生吗小安同学什么意... 小爱同学是女生吗小安同学什么意思 小爱同学,小安同学说你是女生。小安是男的。
内分泌失调导致脸上长斑,怎么调... 内分泌失调导致脸上长斑,怎么调理内分泌失调导致脸上长斑,怎么调理先调理内分泌,去看中医吧,另外用好的...
《魔幻仙境》刺客,骑士人物属性... 《魔幻仙境》刺客,骑士人物属性加点魔幻仙境骑士2功1体质
很喜欢她,该怎么办? 很喜欢她,该怎么办?太冷静了!! 太理智了!爱情是需要冲劲的~不要考虑着考虑那~否则缘...
言情小说作家 言情小说作家我比较喜欢匪我思存的,很虐,很悲,还有梅子黄时雨,笙离,叶萱,还有安宁的《温暖的玄》 小...
两个以名人的名字命名的风景名胜... 两个以名人的名字命名的风景名胜?快太白楼,李白。尚志公园,赵尚志。
幼儿教育的代表人物及其著作 幼儿教育的代表人物及其著作卡尔威特的《卡尔威特的教育》,小卡尔威特,他儿子成了天才后写的《小卡尔威特...
海贼王中为什么说路飞打凯多靠霸... 海贼王中为什么说路飞打凯多靠霸气升级?凯多是靠霸气升级吗?因为之前刚到时确实打不过人家因为路飞的实力...
运气不好拜财神有用吗运气不好拜... 运气不好拜财神有用吗运气不好拜财神有没有用1、运气不好拜财神有用。2、拜财神上香前先点蜡烛,照亮人神...