使用--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 的配置会覆盖它。

相关推荐
lucia_zl14 小时前
pytest并发测试,资源问题导致用例失败解决办法
pytest
鱼鱼说测试17 小时前
Selenium4+Pytest自动化测试框架实战
pytest
XYiFfang7 天前
【Pytest】解决Pytest中Teardown钩子的TypeError:实例方法与类方法的调用差异
python·pytest
Kingairy9 天前
Pytest 插件:pytest_runtest_protocol
python·pytest
AIZHINAN9 天前
Pytest 插件介绍和开发
测试工具·pytest·插件开发
灰阳阳11 天前
替身演员的艺术:pytest-mock 从入门到飙戏
自动化测试·python·pytest·unit testing·pytest-mock
年年测试14 天前
Playwright与PyTest结合指南
pytest
focksorCr15 天前
pytest 并发执行用例(基于受限的测试资源)
python·pytest
律品19 天前
pytest的前置与后置
开发语言·python·pytest