不积跬步,无以至千里
博客
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 16:46:28 阅读:88 次 消耗积分:0 分
[【原文链接】Pytest----如何正确的配置使用日志功能](http://devops-dev.com/article/482) 在pytest自动化测试中,如果只是简单的从应用的角度来说,完全可以不去了解pytest中的显示信息的部分以及原理,完全可以通过使用推荐的pytest.ini配置,从而可以做到相对来说比较通用的日志配置。 这里我们推荐使用如下配置,其中log_cli 相关的四条配置是用来配置live log即实时日志的,而其他三条配置则是用例配置capture log即捕获日志的。分别对他们的日志级别、日志格式、时间戳格式进行了设置,比如这里日志级别都设置为info,当然如果脚本稳定之后,提交自动化测试代码仓库时可以将日志级别调整为warning。 ```bash [pytest] log_cli = True log_cli_level = info log_cli_format = %(asctime)s | %(levelname)s | %(filename)s:%(lineno)s | %(message)s" log_cli_date_format = %Y-%m-%d %H:%M:%S log_level = info log_format = %(asctime)s | %(levelname)s | %(filename)s:%(lineno)s | %(message)s" log_date_format = %Y-%m-%d %H:%M:%S ``` 下面用一个简单的测试脚本来展示一下上述日志配置的效果。 ```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 ``` 执行结果如下,可以看出这里显示了实时日志(live log),同时未显示debug级别的日志,捕获日志(capture log)同样也未显示debug级别的日志,而且时间戳和日志格式相对来说都是比较符合实际应用的,因此这里的推荐的pytest.ini中对日志的配置,完全可以拿去直接使用。 ```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-06 00:42:06 | INFO | test_demo.py:5 | this is info log ..." 2022-12-06 00:42:06 | WARNING | test_demo.py:6 | this is warning log ..." 2022-12-06 00:42:06 | ERROR | test_demo.py:7 | this is error log ..." 2022-12-06 00:42:06 | CRITICAL | test_demo.py:8 | 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 -------------------- 2022-12-06 00:42:06 | INFO | test_demo.py:5 | this is info log ..." 2022-12-06 00:42:06 | WARNING | test_demo.py:6 | this is warning log ..." 2022-12-06 00:42:06 | ERROR | test_demo.py:7 | this is error log ..." 2022-12-06 00:42:06 | CRITICAL | 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.07s ==================== (demo-HCIhX0Hq) E:\demo> ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/482
上一篇:
Pytest----caplog的应用场景以及使用方法
下一篇:
Pytest----插件pytest-attrib即根据属性挑选用例的用法
你的昵称:
你的评论:
提示:登录后添加有效评论可享受积分哦!
点此登录
搜索
个人成就
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Go语言官方文档
Docker官方文档
Jenkins中文用户手册
Markdown语法官方教程
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件