不积跬步,无以至千里
博客
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----如何控制一个测试文件产生的告警都忽略或者转换为报错
收藏本文
作者:redrose2100 类别:Pytest 日期:2022-12-03 09:05:53 阅读:65 次 消耗积分:0 分
[【原文链接】Pytest----如何控制一个测试文件产生的告警都忽略或者转换为报错](http://devops-dev.com/article/471) 在有些场景下,希望对一个测试文件中的所有的测试方法中的告警进行忽略不显示或者将所有的告警转换为错误,此时只需要在文件开头定义pytestmark即可,如下代码中在定义pytestmark中使用ignore参数,表示当前文件中的所有告警都忽略不显示。 ```python import pytest import warnings pytestmark = pytest.mark.filterwarnings("ignore") 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 ``` 执行结果如下,可以看出此时文件中定义的两个告警均为显示。 ```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 ... . ==================== 2 passed in 0.02s ==================== (demo-HCIhX0Hq) E:\demo> ``` 如下,即定义pytestmark的时候指定error,则可以将当前文件中的所有告警均转换为报错。 ```python import pytest import warnings pytestmark = pytest.mark.filterwarnings("error") 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 ``` 执行结果如下所示,即两个用例均报错了,报错原因就是告警的原因。 ```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 ... 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:7: 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:13: 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类型的告警进行忽略。 ```python import pytest import warnings pytestmark = pytest.mark.filterwarnings("ignore::UserWarning") 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 ``` 执行结果如下,可以看出此时UserWarning的告警已经不显示了,而SyntaxWarning类型的告警依然是显示的。 ```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_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 ============== 2 passed, 1 warning in 0.02s =============== (demo-HCIhX0Hq) E:\demo> ``` 同样地这里也是支持当当前文件中的所有告警的内容进行正则匹配,如下即从告警信息中匹配demo字符的,匹配到的就忽略不显示。 ```python import pytest import warnings pytestmark = pytest.mark.filterwarnings("ignore:.*demo.*") 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的UserWarning类型的告警已经不显示了,而不含demo的SyntaxWarning类型的的告警依然会显示。 ```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_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 ============== 2 passed, 1 warning in 0.02s =============== (demo-HCIhX0Hq) E:\demo> ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/471
上一篇:
Pytest----如何通过filterwarnings配置不显示告警或将告警报错
下一篇:
Pytest----如何关闭所有告警显示
你的昵称:
你的评论:
提示:登录后添加有效评论可享受积分哦!
点此登录
搜索
个人成就
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Go语言官方文档
Docker官方文档
Jenkins中文用户手册
Markdown语法官方教程
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件