不积跬步,无以至千里
博客
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-05 07:08:32 阅读:83 次 消耗积分:0 分
[【原文链接】Pytest----什么是实时日志和捕获日志](http://devops-dev.com/article/479) 实时日志和捕获日志与实时标准输出和捕获标准输出是类似的,不同的是实时日志和捕获日志都是用来显示通过logging模块打印的的日志内容,而对于比如print打印的内容时不会在实时日志和捕获日志中显示的。 实时日志顾名思义,就是实时显示的日志,而捕获日志就是在执行的过程中将logging打印的日志都收集起来,同样pytest的对捕获日志的显示机制是当测试脚本失败后才会显示,而当用例通过时,则是不会显示捕获日志的,这与实际情况时一致的,因为在自动化测试过程中,当用例都通过后,我们一般是不关心日志打印的,只有当用例失败后我们在定位失败原因的时候才会去关注出错的信息以及帮助定位问题的日志信息。有一点不同额是在pytest中实时日志在默认情况下是不显示的,如果要显示实时日志需要主动配置开启实时日志。 下面首先观察如下代码,即在测试脚本中通过logging打印了几条日志信息。用例断言设置通过即用例执行会通过。 ```python import logging def test_demo(): logging.warning("this is warning log ...") logging.error("this is error log ...") logging.critical("this is critical log ...") assert 1==1 ``` 执行结果如下,可以发现虽然此时日志打印的warning、error和critical日志,按正常来说应该是可以显示出来的,但是这里却啥也没显示,这就是说在pytest中默认情况下实时日志是关闭的。 ```bash (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 plugins: assume-2.4.3, rerunfailures-10.2 collected 1 item test_demo.py . [100%] ==================== 1 passed in 0.02s ==================== (demo-HCIhX0Hq) E:\demo> 这里可以先通过pytest –o log_cli=true 执行,观察结果如下,此时可以看到已经存在live log call了,这就是实时日志。 (demo-HCIhX0Hq) E:\demo>pytest -o log_cli=true =================== 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::test_demo ---------------------- live log call ---------------------- WARNING root:test_demo.py:4 this is warning log ... ERROR root:test_demo.py:5 this is error log ... CRITICAL root:test_demo.py:6 this is critical log ... PASSED [100%] ==================== 1 passed in 0.02s ==================== (demo-HCIhX0Hq) E:\demo> ``` 下面将测试用例断言设置为失败。 ```python import logging def test_demo(): logging.warning("this is warning log ...") logging.error("this is error log ...") logging.critical("this is critical log ...") assert 1==2 ``` 执行结果如下,可以发现,live log call即实时日志会在执行用例的时候直接实时显示出来,而在用例执行失败后,会有个Captured log call的部分,这就是捕获日志,即将用例中通过logging打印的日志信息收集起来,待用例执行完成后在打印出来。 ```bash (demo-HCIhX0Hq) E:\demo>pytest -o log_cli=true =================== 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::test_demo ---------------------- live log call ---------------------- WARNING root:test_demo.py:4 this is warning log ... ERROR root:test_demo.py:5 this is error log ... CRITICAL root:test_demo.py:6 this is critical log ... FAILED [100%] ======================== FAILURES ========================= ________________________ test_demo ________________________ def test_demo(): logging.warning("this is warning log ...") logging.error("this is error log ...") logging.critical("this is critical log ...") > assert 1==2 E assert 1 == 2 test_demo.py:7: AssertionError -------------------- Captured log call -------------------- WARNING root:test_demo.py:4 this is warning log ... ERROR root:test_demo.py:5 this is error log ... CRITICAL root:test_demo.py:6 this is critical log ... ================= short test summary info ================= FAILED test_demo.py::test_demo - assert 1 == 2 ==================== 1 failed in 0.08s ==================== (demo-HCIhX0Hq) E:\demo> ``` 上面通过实例展示了实时日志与捕获日志。
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/479
上一篇:
Pytest----如何使用logging模块写日志
下一篇:
Pytest----如何打开或关闭实时日志和捕获日志
你的昵称:
你的评论:
提示:登录后添加有效评论可享受积分哦!
点此登录
搜索
个人成就
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Go语言官方文档
Docker官方文档
Jenkins中文用户手册
Markdown语法官方教程
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件