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

相关推荐
姚青&1 天前
Pytest 测试用例编写
测试用例·pytest
Warren981 天前
Pytest Fixture 作用域与接口测试 Token 污染问题实战解析
功能测试·面试·单元测试·集成测试·pytest·postman·模块测试
serve the people3 天前
python环境搭建 (七) pytest、pytest-asyncio、pytest-cov 试生态的核心组合
开发语言·python·pytest
真智AI3 天前
用 LLM 辅助生成可跑的 Python 单元测试:pytest + coverage 覆盖率报告(含运行指令与排坑)
python·单元测试·pytest
Warren984 天前
Allure 常用装饰器:实战用法 + 最佳实践(接口自动化)
运维·服务器·git·python·单元测试·自动化·pytest
Warren985 天前
Pytest Fixture 到底该用 return 还是 yield?
数据库·oracle·面试·职场和发展·单元测试·pytest·pyqt
Warren985 天前
Pytest Fixture 作用域详解:Function、Class、Module、Session 怎么选
面试·职场和发展·单元测试·pytest·pip·模块测试·jira
我的xiaodoujiao6 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--将自动化测试结果自动推送至钉钉工作群聊
前端·python·测试工具·ui·pytest
我的xiaodoujiao7 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 45--生成项目需要的requirements.txt依赖文件
python·学习·测试工具·pytest
月明长歌7 天前
全栈测试修炼指南:从接口策略到 Python+Pytest+Allure 企业级架构
python·架构·pytest