当前位置: 首页 > news >正文

pytest日志显示

pytest 中,可以通过 钩子函数配置文件 pytest.ini 配置日志的显示方式,实现对日志的灵活控制。以下是常用实现方式及配置说明。


方式一:使用 conftest.py 钩子函数自定义日志显示

通过 conftest.py 文件中的钩子函数,实现日志记录并自定义显示。

实现步骤
import pytest
import logging# 配置日志格式和输出
def pytest_configure(config):log_format = "%(asctime)s - %(levelname)s - %(message)s"logging.basicConfig(level=logging.INFO,  # 设置日志级别format=log_format,filename="test.log",  # 日志文件保存路径filemode="w"  # 每次运行时重写日志文件)console = logging.StreamHandler()  # 控制台输出console.setLevel(logging.INFO)  # 控制台日志级别console.setFormatter(logging.Formatter(log_format))logging.getLogger("").addHandler(console)logging.info("Logging setup complete.")# 钩子函数:记录每个测试的开始和结束
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_protocol(item, nextitem):logging.info(f"Test started: {item.name}")outcome = yieldresult = "PASSED" if outcome.get_result().passed else "FAILED"logging.info(f"Test finished: {item.name} - {result}")
运行效果

运行测试时,日志会记录在 test.log 文件中,同时在控制台实时输出。


方式二:通过 pytest.ini 配置文件管理日志显示

使用 pytest.ini 文件设置全局日志配置,省去手动编写 logging 的代码。

配置示例

创建或编辑项目中的 pytest.ini 文件:

[pytest]
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  # 日期格式log_file = test.log  # 日志文件保存路径
log_file_level = INFO  # 文件日志级别
log_file_format = %(asctime)s - %(levelname)s - %(message)s  # 文件日志格式
log_file_date_format = %Y-%m-%d %H:%M:%S  # 文件日期格式
运行效果
  • 控制台输出的日志格式与级别按照配置显示。
  • 所有日志信息保存到 test.log 文件。

方式三:在测试代码中使用 caplog 捕获日志

使用 pytest 提供的内置 caplog 功能捕获日志,适用于验证日志输出的测试场景。

测试用例示例
import loggingdef test_logging_example(caplog):logging.info("This is an info log.")logging.error("This is an error log.")assert "info log" in caplog.text  # 验证日志内容assert "error log" in caplog.text
运行效果
  • caplog 捕获到的日志信息可以用于断言和分析。

常见日志配置说明

配置项描述示例值
log_cli启用控制台日志输出。true
log_cli_level设置控制台日志输出的级别。INFO, DEBUG, ERROR
log_cli_format控制台日志的格式。%(asctime)s - %(message)s
log_cli_date_format控制台日志的日期格式。%Y-%m-%d %H:%M:%S
log_file指定日志文件的路径。test.log
log_file_level设置日志文件的输出级别。INFO, WARNING
log_file_format文件日志的格式。%(levelname)s - %(message)s
log_file_date_format文件日志的日期格式。%Y-%m-%d %H:%M:%S

推荐组合方式

  1. 全局日志管理:

    • 使用 pytest.ini 配置,统一管理日志级别、格式和输出路径。
    • 适用于简单、稳定的项目日志需求。
  2. 细粒度控制:

    • conftest.py 中结合钩子函数,针对用例或阶段添加特定的日志逻辑。
    • 适用于复杂场景的动态日志需求。
  3. 日志验证:

    • 使用 caplog 在特定测试用例中捕获日志内容,便于断言和调试。

通过以上方式,可以灵活、高效地在 pytest 项目中实现日志管理与显示。

http://www.lryc.cn/news/517119.html

相关文章:

  • 【信息系统项目管理师】第15章:项目风险管理过程详解
  • Diffusers 使用 LoRA
  • 云安全博客阅读(二)
  • SpringCloud系列教程:微服务的未来(六)docker教程快速入门、常用命令
  • Vue 快速入门:开启前端新征程
  • UVM:uvm_component methods configure
  • LLM 训练中存储哪些矩阵:权重矩阵,梯度矩阵,优化器状态
  • 大模型思维链推理的进展、前沿和未来分析
  • NLP 技术的突破与未来:从词嵌入到 Transformer
  • 嵌入式中QT实现文本与线程控制方法
  • 云备份项目--服务端编写
  • Node.js——fs(文件系统)模块
  • SAP BC 同服务器不同client之间的传输SCC1
  • CentOS: RPM安装、YUM安装、编译安装(详细解释+实例分析!!!)
  • linux音视频采集技术: v4l2
  • MySQL使用navicat新增触发器
  • voice agent实现方案调研
  • TCP通信原理学习
  • Three.js 基础概念:构建3D世界的核心要素
  • 如何用代码提交spark任务并且获取任务权柄
  • 关于Mac中的shell
  • 【npm依赖包介绍】借助rimraf依赖包,在用npm run build构建项目时,清空dist目录,避免新旧混合
  • 爬虫学习记录
  • Java Spring Boot实现基于URL + IP访问频率限制
  • C4D2025 win版本安装完无法打开,提示请将你的maxon App更新至最新版本,如何解决
  • 微信小程序实现登录注册
  • SpringBoot环境和Maven配置
  • 大语言模型训练所需的最低显存,联邦大语言模型训练的传输优化技术
  • 1.07 标准IO
  • 恒压恒流原边反馈控制芯片 CRE6289F