Pytes正确的配置使用日志功能
作者:redrose2100 发布时间:2023-07-18 05:51:17
标签:Pytes,日志,配置
在pytest自动化测试中,如果只是简单的从应用的角度来说,完全可以不去了解pytest中的显示信息的部分以及原理,完全可以通过使用推荐的pytest.ini配置,从而可以做到相对来说比较通用的日志配置。
这里我们推荐使用如下配置,其中log_cli 相关的四条配置是用来配置live log即实时日志的,而其他三条配置则是用例配置capture log即捕获日志的。分别对他们的日志级别、日志格式、时间戳格式进行了设置,比如这里日志级别都设置为info,当然如果脚本稳定之后,提交自动化测试代码仓库时可以将日志级别调整为warning。
[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
下面用一个简单的测试脚本来展示一下上述日志配置的效果。
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中对日志的配置,完全可以拿去直接使用。
(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>
来源:https://blog.csdn.net/redrose2100/article/details/128195838
0
投稿
猜你喜欢
- LOAD DATA INFILE '文件地址' INTO TABLE 表名 FIELDS TERMINATED BY
- PHP chr() 函数实例从不同 ASCII 值返回字符:<?php echo chr(52) . "<br>
- 以下是menu.asp代码 程序代码 <% '-----------------------------------
- 本文介绍使用ADODB.Stream组件来下载服务器文件,例如:download.asp?file=相对路径的文件。就可以把这个文件下载下来
- MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由
- 代码如下:<% str = request("str") reg 
- 创建Spring Boot应用在基于Spring来重构JSP应用之前,我们先引入Spring Boot,使之成为一个Spring Boot应
- check.asp 代码如下:<% '''''''''
- 最近重新温习了一次《javascript设计模式》,确实是一本好书,每次看都有不同的领悟,每次领悟到的都受益匪浅,无怪古圣人都说学无止镜了,
- 最近做了wap站中的搜索结果页的改版,记录一下关于锚点链接的心得~关于锚点链接锚点链接一般用于比较长的网页,使用内部链接建立页内目录。单击目
- 读《论语》,子张十九,子夏曰:博学而笃志,切问而近思,仁在其中矣。 博学:架构需要广度,要尽量多学习各方面的知识。笃志:除了广度,架构师还需
- 也不一定,以前从来没有深入的研究过sql查询,最近买了一本T-SQL查询的书,把以前忽视的问题都记录一下 以前一直模模糊糊的把sqlserv
- 代码如下:using System; using System.Data; using System.Configuration
- 如下图,我们在做图片logo列表的时候通常是用li标签来实现。html:<ul class="logolist&q
- 前言在写程序时,我们会经常碰到程序出现异常,这时候我们就不得不处理这些异常,以保证程序的健壮性。处理异常的版本有以下几种,你通常的做法是哪种
- 关于跨域这个话题,很早就答应过要分享,但是因为懒,一直拖着,直到D2上有人谈起了“完美跨域”。“跨域”应该已经算不上什么难题了,只是提起“完
- div+css实现圆角边框,在网络上查看了一下,很多都是实现圆角的矩形的方法,我在这里介绍的是实现圆角矩形边框的方法。用代码说明问题:<
- 本文实例介绍了使用javascript来经验表单数据的方法,如:校验是否为英文,校验是否为数字及校验IP地址等: &l
- asp之字符串函数示例用字符串函数对字符串进行截头去尾、大小写替换等操作。函数语 * 能LenLen(string|varname)返回字符串
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 12 - Drag and Drop using