不积跬步,无以至千里
博客
Python
Python
Flask
Django
FastAPI
设计模式(Python语言)
数据结构与算法(Python语言)
爬虫
数据分析
Java
Java
SpringBoot
SprintCloud
CC++
C语言
C++语言
Go
Go
设计模式(Go)
数据结构与算法(Go)
云计算
云计算理论
Linux
Shell
云原生
云原生理论
Docker
Kubernetes
Kubersphere
DevOps
Git
Gitlab
Jenkins
Nexus
Harbor
SonarQube
Grafana
OpenQA
建木
禅道
Compass-CI
前端
HTML
CSS
JavaScript
BootStrap
Vue
React
Markdown
数据库
MySql
Redis
MongoDB
H2 Database
Liquibase
ElasticStack
中间件
MQ
Kafka
Nginx
cpolar
阿里云
测试
测试理论
安全测试
压力测试
Pytest
UnitTest
考试
软考中级(软件设计师考试)
软考高级(系统架构设计师考试)
登录
注册
Pytest----如何通过filterwarnings配置不显示告警或将告警报错
收藏本文
作者:redrose2100 类别:Pytest 日期:2022-12-03 07:29:25 阅读:60 次 消耗积分:0 分
[【原文链接】Pytest----如何通过filterwarnings配置不显示告警或将告警报错](http://devops-dev.com/article/470) 首先在脚本中人工抛出一条告警,测试代码如下所示: ```python import pytest import warnings def test_demo1(): print("in test_demo1 ...") warnings.warn(SyntaxWarning("warning,used to test...")) assert 1==1 ``` 执行结果如下所示,即这里在执行结果中也显示了一条告警信息。 ```bash (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后面是两个冒号。 ```python 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 ``` 执行结果如下,即不再有告警信息。 ```bash (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即可,如下: ```python 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 ``` 执行结果如下,可以看到此时用例失败了,原因就是这个告警。 ```bash (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中的告警则会显示。 ```python 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中的告警并未显示。 ```bash (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> ``` 同理,将告警转换为报错信息,也是同样支持正则表达式的,比如如下代码: ```python 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中的告警并未匹配到正则表达式,因此不会报错,所以继续报出了告警信息。 ```bash (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> ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/470
上一篇:
Pytest----命令行如何控制不显示告警或将告警报错
下一篇:
Pytest----如何控制一个测试文件产生的告警都忽略或者转换为报错
你的昵称:
你的评论:
提示:登录后添加有效评论可享受积分哦!
点此登录
搜索
个人成就
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Go语言官方文档
Docker官方文档
Jenkins中文用户手册
Markdown语法官方教程
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件