不积跬步,无以至千里
博客
Python
Python
Python代码集
Flask
Django
FastAPI
Scrapy
爬虫
设计模式(Python语言)
数据结构与算法(Python语言)
数据分析
Java
Java
SpringBoot
SprintCloud
CC++
C语言
C++语言
Go
Go
设计模式(Go)
数据结构与算法(Go)
云计算
云计算理论
Linux
Shell
云原生
云原生理论
Docker
Dockerfile集合
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
Selenium
UnitTest
Appium
SikuliX
Airtest
Fiddler
os自动化测试
考试
软考中级(软件设计师考试)
软考高级(系统架构设计师考试)
登录
注册
Pytest----如何对产生的告警进行断言
收藏本文
作者:redrose2100 类别:Pytest 日期:2022-12-03 18:30:06 阅读:100 次 消耗积分:0 分
[【原文链接】Pytest----如何对产生的告警进行断言](http://devops-dev.com/article/474) 对告警进行断言,可以通过with语句对告警的类型进行断言,如下,即断言代码中会产生UserWarning类型的告警。 ```python import pytest import warnings def test_demo(): print("in test_demo ...") with pytest.warns(UserWarning): warnings.warn(UserWarning("warning,used to demo...")) ``` 执行结果如下,即断言通过。 ```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, configfile: pytest.ini plugins: assume-2.4.3, rerunfailures-10.2 collected 1 item test_demo.py in test_demo ... . ==================== 1 passed in 0.02s ==================== (demo-HCIhX0Hq) E:\demo> ``` 将测试代码修改为如下代码,即在代码中抛出SyntaxWarning类型的告警,而断言期望的是UserWarning类型的告警。 ```python import pytest import warnings def test_demo(): print("in test_demo ...") with pytest.warns(UserWarning): warnings.warn(SyntaxWarning("warning,used to demo...")) ``` 执行结果如下,即此时报错了,显示并未有UserWarning类型的告警产生。 ```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, configfile: pytest.ini plugins: assume-2.4.3, rerunfailures-10.2 collected 1 item test_demo.py in test_demo ... F ======================== FAILURES ========================= ________________________ test_demo ________________________ def test_demo(): print("in test_demo ...") with pytest.warns(UserWarning): > warnings.warn(SyntaxWarning("warning,used to demo...")) E Failed: DID NOT WARN. No warnings of type (
,) were emitted. E The list of emitted warnings is: [SyntaxWarning('warning,used to demo...')]. test_demo.py:8: Failed ================= short test summary info ================= FAILED test_demo.py::test_demo - Failed: DID NOT WARN. No warnings of type (
``` 此外,还可以通过正则表达式对告警内容进行断言,如下即判断告警内容中含有demo。 ```python import pytest import warnings def test_demo(): print("in test_demo ...") with pytest.warns(UserWarning,match=r'.*demo.*'): warnings.warn(UserWarning("warning,used to demo...")) ``` 执行结果如下,即断言通过。 ```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, configfile: pytest.ini plugins: assume-2.4.3, rerunfailures-10.2 collected 1 item test_demo.py in test_demo ... . ==================== 1 passed in 0.02s ==================== (demo-HCIhX0Hq) E:\demo> ``` 初次以为,还可以对函数调用中的告警类型进行断言,如下即断言add函数中会产生UserWarning类型的告警,同时,add函数需要给定两个参数a和b。 ```python import pytest import warnings def add(a,b): print("a:",a) print("b:",b) warnings.warn(UserWarning("warning,used to demo...")) def test_demo(): print("in test_demo ...") pytest.warns(UserWarning,add,10,20) ``` 执行结果如下,可以看出,参数a和b能够正常传入,而且也做到了对函数中产生的告警类型的断言。 ```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, configfile: pytest.ini plugins: assume-2.4.3, rerunfailures-10.2 collected 1 item test_demo.py in test_demo ... a: 10 b: 20 . ==================== 1 passed in 0.02s ==================== (demo-HCIhX0Hq) E:\demo> ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/474
上一篇:
Pytest----如何通过pytest.ini配置不显示告警或将告警报错
下一篇:
Pytest----如何通过recwarn记录用例中产生的告警
你的昵称:
你的评论:
提示:登录后添加有效评论可享受积分哦!
点此登录
搜索
个人成就
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Go语言官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
Markdown语法官方教程
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件