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

相关推荐
黄昏恋慕黎明9 小时前
博客论坛技术迭代
python·pytest
2601_9620782311 小时前
Python接口自动化流程(pytest)
python·pytest·接口自动化·测试框架·断言
杨女士YRJ11 小时前
python+pytest+01
python·pytest
2601_962078232 天前
Python接口自动化框架:pytest
python·pytest·接口自动化·测试框架·restfulapi
2601_962078192 天前
Appium+Python+pytest自动化测试框架详解
自动化测试·python·appium·pytest·移动应用
2601_962284503 天前
安卓APP UI自动化测试:Python + UiAutomator2 + pytest + pytest-html
python·pytest·uiautomator2·ui自动化测试·安卓app
2601_962300813 天前
基于pytest的接口自动化测试:使用requests模块的实战项目与接口关联方法详解
接口自动化测试·测试用例·pytest·接口关联·requests模块
2601_962300813 天前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests
11路没有终点4 天前
Pytest 安装、版本与第一个测试用例
测试用例·pytest
11路没有终点5 天前
pytest 参数化与 Mark 标记
pytest