不积跬步,无以至千里
博客
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 09:23:19 阅读:72 次 消耗积分:0 分
[【原文链接】Pytest----如何打开或关闭实时日志和捕获日志](http://devops-dev.com/article/480) Pytest中默认情况下实时日志是关闭的,捕获日志是打开的,因此这里主要介绍如何打开实时日志以及关闭捕获日志。 比如如下脚本: ```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 ``` 开启实时日志有两种方式,一种是通过命令行,pytest –o log_cli=true,如下所示: ```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 ... PASSED [100%] ==================== 1 passed in 0.02s ==================== (demo-HCIhX0Hq) E:\demo> ``` 在命令行中打开实时日志是比较繁琐的,因此另外一种方式就是在pytest.ini文件中配置。创建pytest.ini文件,然后写入如下配置即可打开实时日志。 ```bash [pytest] log_cli = True ``` 此时直接使用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, configfile: pytest.ini 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> ``` 当不设置日志级别时,默认情况下也是warning,即warning及以上的级别日志才会显示,info和debug日志在默认情况下是不会显示的,比如在测试脚本中增加info和debug的日志。 ```python import logging def test_demo(): logging.debug("this is debug log ...") logging.info("this is info log ...") logging.warning("this is warning log ...") logging.error("this is error log ...") logging.critical("this is critical log ...") assert 1==1 ``` 执行结果如下,可以看到此时info和debug日志是未显示的。 ```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, configfile: pytest.ini plugins: assume-2.4.3, rerunfailures-10.2 collected 1 item test_demo.py::test_demo ---------------------- live log call ---------------------- WARNING root:test_demo.py:6 this is warning log ... ERROR root:test_demo.py:7 this is error log ... CRITICAL root:test_demo.py:8 this is critical log ... PASSED [100%] ==================== 1 passed in 0.02s ==================== (demo-HCIhX0Hq) E:\demo> ``` 在pytest.ini中开启实时日志的同时,可以同时将日志级别以及日志格式同步设置一下,不如pytest.ini设置如下配置。 ```bash [pytest] log_cli = True log_cli_level = debug log_cli_format = %(asctime)s %(levelname)s %(message)s log_cli_date_format = %Y-%m-%d %H:%M:%S ``` 此时再次执行,即debug和info日志此时均显示出来了,而且每行日志同时带有时间戳、日志级别、日志内容了。 ```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, configfile: pytest.ini plugins: assume-2.4.3, rerunfailures-10.2 collected 1 item test_demo.py::test_demo ---------------------- live log call ---------------------- 2022-12-05 16:22:17 DEBUG this is debug log ... 2022-12-05 16:22:17 INFO this is info log ... 2022-12-05 16:22:17 WARNING this is warning log ... 2022-12-05 16:22:17 ERROR this is error log ... 2022-12-05 16:22:17 CRITICAL this is critical log ... PASSED [100%] ==================== 1 passed in 0.02s ==================== (demo-HCIhX0Hq) E:\demo> ``` 捕获日志默认情况下是开启的,即当用例失败时就会自动显示捕获的日志了,比如如下将测试用例修改为失败。 ```python import logging def test_demo(): logging.debug("this is debug log ...") logging.info("this is info log ...") logging.warning("this is warning log ...") logging.error("this is error log ...") logging.critical("this is critical log ...") assert 1==2 ``` 执行结果如下,即用例失败后显示了Captured log call即捕获日志。 ```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, configfile: pytest.ini plugins: assume-2.4.3, rerunfailures-10.2 collected 1 item test_demo.py::test_demo ---------------------- live log call ---------------------- 2022-12-05 16:28:59 DEBUG this is debug log ... 2022-12-05 16:28:59 INFO this is info log ... 2022-12-05 16:28:59 WARNING this is warning log ... 2022-12-05 16:28:59 ERROR this is error log ... 2022-12-05 16:28:59 CRITICAL this is critical log ... FAILED [100%] ======================== FAILURES ========================= ________________________ test_demo ________________________ def test_demo(): logging.debug("this is debug log ...") logging.info("this is info log ...") 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:9: AssertionError -------------------- Captured log call -------------------- DEBUG root:test_demo.py:4 this is debug log ... INFO root:test_demo.py:5 this is info log ... WARNING root:test_demo.py:6 this is warning log ... ERROR root:test_demo.py:7 this is error log ... CRITICAL root:test_demo.py:8 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> ``` 捕获日志pytest默认是打开的,这里顺便也提一下,对于捕获日志,也是可以通过在pytest.ini中配置日志级别、时间戳、格式等的,而且捕获日志配置与实时日志配置关键字是不一样的,需要注意分开,如下为配置捕获日志格式的。 ```bash [pytest] log_level = debug log_format = %(asctime)s %(levelname)s %(message)s log_date_format = %Y-%m-%d %H:%M:%S 执行结果如下,可以看到此时捕获日志已经将debug和info级别的日志显示出来了,并且也带有时间戳和日志级别的信息。 (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 1 item test_demo.py F [100%] ======================== FAILURES ========================= ________________________ test_demo ________________________ def test_demo(): logging.debug("this is debug log ...") logging.info("this is info log ...") 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:9: AssertionError -------------------- Captured log call -------------------- 2022-12-05 17:12:33 DEBUG this is debug log ... 2022-12-05 17:12:33 INFO this is info log ... 2022-12-05 17:12:33 WARNING this is warning log ... 2022-12-05 17:12:33 ERROR this is error log ... 2022-12-05 17:12:33 CRITICAL 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> ``` 而若想关闭捕获日志,则只需要使用--show-capture=no 参数即可,比如如下,可以看到,此时即没有捕获日志显示了,但是在实际应用中,一般不需要将捕获日志关闭,因为在默认用例通过时捕获日志就是不显示的,而当用例失败时,此时正是需要查看捕获日志的,因此对于捕获日志一般保持默认打开即可,需要做的就是去设置一下日志打印的格式等。 ```bash (demo-HCIhX0Hq) E:\demo>pytest --show-capture=no =================== 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 F [100%] ======================== FAILURES ========================= ________________________ test_demo ________________________ def test_demo(): logging.debug("this is debug log ...") logging.info("this is info log ...") 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:9: AssertionError ================= short test summary info ================= FAILED test_demo.py::test_demo - assert 1 == 2 ==================== 1 failed in 0.07s ==================== (demo-HCIhX0Hq) E:\demo> ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/480
上一篇:
Pytest----什么是实时日志和捕获日志
下一篇:
Pytest----caplog的应用场景以及使用方法
你的昵称:
你的评论:
提示:登录后添加有效评论可享受积分哦!
点此登录
搜索
个人成就
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Go语言官方文档
Docker官方文档
Jenkins中文用户手册
Markdown语法官方教程
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件