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

相关推荐
在世修行2 天前
干货:pytest
pytest·fixture·pytest assert
小小龙学IT4 天前
Python pytest 测试框架深度解析:从断言重写到插件生态的工程化测试体系
网络·python·pytest
落魄大学生之流水线上谋生计6 天前
pytest 入门与实践:从第一个测试到可维护的测试体系
pytest
念越7 天前
Allure 测试报告保姆级教程:从安装到 pytest 集成与报告生成
pytest·allure
念越8 天前
接口自动化测试从入门到实战:接口用例设计、Requests、Pytest、YAML、JSON Schema 与 Allure 报告
python·测试工具·自动化·json·pytest
霍格沃兹测试学院-小舟畅学12 天前
AI+Swagger:一键生成500条pytest接口用例
人工智能·pytest
把头塞进显示器21 天前
电商平台-测试报告
python·selenium·pytest
昔我往昔25 天前
pytest日志问题排查记录
python·pytest
黄昏恋慕黎明1 个月前
博客论坛技术迭代
python·pytest
2601_962078231 个月前
Python接口自动化流程(pytest)
python·pytest·接口自动化·测试框架·断言