Pytest----如何正确使用pytest的日志功能

简介: Pytest----如何正确使用pytest的日志功能

【原文链接】

一、在pytest.ini配置如下配置

cli live log 部分配置是实时日志,即自动化脚本执行的过程中实时输出,方便调试脚本使用,capture log则是当脚本失败了,会在执行日志的最后将失败的用例中的日志打印输出,便于定位失败的脚本,尤其在执行大量的脚本时,实时日志很难找到报错的位置,capture log则把失败的日志在最后重新打印一遍,如此则非常方便问题定位

[pytest]
# cli live log
log_cli = True
log_cli_level = info
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_date_format = %Y-%m-%d %H:%M:%S
# capture log
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S
log_level = info

二、脚本中使用方法

直接导入logging,然后直接调用logging的info,error,warnning等方法写日志即可,如下演示一个成功的测试函数,一个失败的测试函数,capture log只会捕获失败的用例的日志

import logging

def test_func1():
    logging.info("info log in test_func1...")
    logging.error("error log in test_func1...")
    logging.warning("warnning log in test_func1...")

def test_func2():
    logging.info("info log in test_func2...")
    logging.error("error log in test_func2...")
    logging.warning("warnning log in test_func2...")
    assert 1==2

执行结果如下:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_demo.py::test_func1
---------------------------------------------------------------------------- live log call -----------------------------------------------------------------------------
2021-12-28 10:21:51 INFO info log in test_func1...
2021-12-28 10:21:51 ERROR error log in test_func1...
2021-12-28 10:21:51 WARNING warnning log in test_func1...
PASSED                                                                                                                                                            [ 50%]
test_demo.py::test_func2
---------------------------------------------------------------------------- live log call -----------------------------------------------------------------------------
2021-12-28 10:21:51 INFO info log in test_func2...
2021-12-28 10:21:51 ERROR error log in test_func2...
2021-12-28 10:21:51 WARNING warnning log in test_func2...
FAILED                                                                                                                                                            [100%]

=============================================================================== FAILURES ===============================================================================
______________________________________________________________________________ test_func2 ______________________________________________________________________________

    def test_func2():
        logging.info("info log in test_func2...")
        logging.error("error log in test_func2...")
        logging.warning("warnning log in test_func2...")
>       assert 1==2
E       assert 1 == 2
E         +1
E         -2

test_demo.py:12: AssertionError
-------------------------------------------------------------------------- Captured log call ---------------------------------------------------------------------------
2021-12-28 10:21:51 INFO info log in test_func2...
2021-12-28 10:21:51 ERROR error log in test_func2...
2021-12-28 10:21:51 WARNING warnning log in test_func2...
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_func2 - assert 1 == 2
===================================================================== 1 failed, 1 passed in 0.12s ======================================================================
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
4月前
|
存储 Go
Go 浅析主流日志库:从设计层学习如何集成日志轮转与切割功能
本文将探讨几个热门的 go 日志库如 logrus、zap 和官网的 slog,我将分析这些库的的关键设计元素,探讨它们是如何支持日志轮转与切割功能的配置。
111 0
Go 浅析主流日志库:从设计层学习如何集成日志轮转与切割功能
|
7月前
|
C++
C++-实现日志log功能
C++-实现日志log功能
|
6天前
|
SQL 分布式计算 监控
日志服务主要功能
本文介绍日志服务主要的功能。
15 2
|
7天前
|
缓存 Java 测试技术
Java多线程实战-实现多线程文件下载,支持断点续传、日志记录等功能
Java多线程实战-实现多线程文件下载,支持断点续传、日志记录等功能
|
17天前
|
测试技术 开发者 Python
如何在 Pytest 中添加日志记录
在Pytest中添加日志记录可增强调试和问题排查。通过`pytest.ini`配置文件,设置`log_cli`、`log_cli_level`等参数来控制日志输出。测试用例中导入`logging`库,然后在测试函数中添加日志语句。默认日志显示在控制台,若需写入文件,可调整配置如`log_file`。这样,日志将在运行测试时按指定格式记录到控制台或文件,便于分析测试细节和错误。
19 1
|
7月前
|
存储 运维 监控
如何在 Spring Boot 中设计和实现业务操作日志功能?
如何在 Spring Boot 中设计和实现业务操作日志功能?
1031 4
|
4月前
|
数据可视化 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用相机日志跟踪功能(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用相机日志跟踪功能(C++)
33 0
|
10月前
|
SQL XML 前端开发
Spring Boot + vue-element 开发个人博客项目实战教程(十八、操作日志功能实现)2
Spring Boot + vue-element 开发个人博客项目实战教程(十八、操作日志功能实现)2
91 0
Spring Boot + vue-element 开发个人博客项目实战教程(十八、操作日志功能实现)2
|
5月前
|
设计模式 Java 数据库连接
设计模式与面向对象编程:举例说明在Java中应用工厂模式的场景,并编写一个简单的工厂模式实现。编写一个Java装饰器,用于添加日志记录功能到现有方法上。
设计模式与面向对象编程:举例说明在Java中应用工厂模式的场景,并编写一个简单的工厂模式实现。编写一个Java装饰器,用于添加日志记录功能到现有方法上。
23 0
|
6月前
|
JSON 监控 数据可视化
实时日志分析:通过Golang编写实时日志分析模块,加强公司监控管理软件的日志监控功能
在当今数字化时代,企业面临着庞大而复杂的网络环境,对实时监控和日志分析的需求变得日益迫切。本文将介绍如何使用Golang编写实时日志分析模块,以增强公司监控管理软件的日志监控功能。通过本文的指导,你将能够建立一个定制的实时日志分析系统,更好地监测和管理公司的网络活动。
227 0