使用--log-file保存pytest的运行日志

前面使用了tee和重定向来保存pytest的运行日志,这次使用--log-file,因为它可以配置日志的级别、格式和每行日志的生成时间。

pytest -q -s -ra --count=100 test_open_stream.py --alluredir=./report/CXL --log-file=pytest_log.txt

【pytest.ini】

使用--log-file需要在项目的根目录下创建一个名为 pytest.ini 的文件,编辑文件并添加以下内容设置日志级别、格式、时间等:

复制代码
[pytest]
log_cli = true
log_level = INFO
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S

log_cli: 是否在控制台实时输出日志。

log_level: 指定日志的最小级别。

log_format: 日志的输出格式。

log_date_format: 日志中时间的格式。

【日志级别】

Pytest 使用 Python 的标准日志级别,包括以下几种:

CRITICAL:仅记录关键错误事件,通常是导致程序崩溃或需要立即关注的问题。

ERROR:记录一般错误事件,这些错误通常表示程序运行中的问题。

WARNING:记录警告信息,表示可能的潜在问题,但不会阻止程序运行。

INFO:记录一般信息,用于提供程序的运行状态和步骤的简单描述。

DEBUG:详细记录调试信息,用于诊断和了解程序内部的运行情况。

Pytest 支持 NOTSET 级别(等于0),表示不进行过滤,记录所有日志,包括自定义日志级别。

【优先级】

pytest.ini 优先级最低,pyproject.toml 和 tox.ini 的配置会覆盖它。

相关推荐
思则变12 小时前
[Pytest][Part 1]Pytest 自动化测试框架
pytest
思则变2 天前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
思则变3 天前
[Pytest][Part 3]检测python package状态
pytest
cooldream200916 天前
pytest 框架详解与实战指南
pytest·测试
慕城南风16 天前
【pytest进阶】Pytest之conftest详解
pytest
编程小白gogogo17 天前
AI自动化测试速成(Pytest框架)
pytest
慕城南风17 天前
【pytest进阶】pytest详解及进阶使用
linux·服务器·pytest
Tom Boom24 天前
Pytest断言全解析:掌握测试验证的核心艺术
自动化测试·python·测试开发·pytest
不要一直敲门1 个月前
初学 pytest 记录
pytest