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>

相关内容

热门资讯

天舟九号货运飞船将今日发射 据中国载人航天工程办公室消息,目前,长征七号遥十运载火箭已完成推进剂加注,瞄准北京时间2025年7月...
兴市监处罚〔2025〕53号 兴市监处罚〔2025〕53号浏览:5次基本信息:处罚机关兴城市市场监督管理局处罚日期2025年07月...
(嘉)市监罚〔2025〕综39... (嘉)市监罚〔2025〕综39号浏览:7次基本信息:处罚机关嘉峪关市市场监督管理局处罚日期2025年...
康市监处罚〔2025〕114号 康市监处罚〔2025〕114号浏览:2次基本信息:处罚机关康平县市场监督管理局处罚日期2025年07...
20°C避暑神仙玩法!承包历史... 暑期来昆明想高效逛透城市精华该怎么规划呢下面这条以东风广场为起点的“宝藏圈”线路正合适这里既有穿越百...
大喔饭的创始人是谁? 大喔饭的创始人是谁?CEO是国内著名的青年创业导师林思源先生,毕业汕头大学,未及而立之年,已经是公司...
历史上默默无闻的人 历史上默默无闻的人很多人的是啊
屈玲妮哪里人 屈玲妮哪里人周庄人作为土生土长的周庄人,屈玲妮温婉的外表下却有一股惊人的韧劲。被任命为周庄镇党委书记...
有没很好听的关于暗恋适合男生唱... 有没很好听的关于暗恋适合男生唱的歌? 橘子汽水 很好听 校园风歌名:橘子汽水歌手:南拳妈妈专辑:2...
帮我写下元旦串词。第一个节目《... 帮我写下元旦串词。第一个节目《童年》小合唱。二年级组的。第二个《西班牙斗牛士》舞蹈四年级组。。。第只...