pytest文档85 - logging 日志的使用
前言
pytest 框架有日志管理开关,不需要再添加额外的配置
logging的使用
在用例中添加logging的日志内容
import pytest
import logging
log = logging.getLogger(__name__)
@pytest.fixture
def first():
log.debug('debug message: first to do .........')
def inc(x):
log.info('function inc--------')
return x + 1
def test_answer(first):
log.debug('run case: test_answer')
log.info('test case --------')
assert inc(3) == 4
直接运行pytest是不会用日志输出的,因为默认仅输出warning 以上的级别日志
开启日志
要想看到打印日志,先在pytest.ini 配置开启日志,并且设置日志级别
[pytest]
log_cli = true
log_cli_level = info
运行用例
>pytest test_log.py
=================== test session starts ====================
platform win32 -- Python 3.8.5, pytest-7.2.0, pluggy-1.0.0
collected 1 item
test_log.py::test_answer
------------------- live log call ------------------------
INFO case.test_log:test_log.py:20 test case --------
INFO case.test_log:test_log.py:14 function inc--------
PASSED
于是在控制台就可以看到打印的日志内容了,日志的格式和时间格式也可以设置
[pytest]
log_cli = true
log_cli_level = info
log_cli_format = %(asctime)s %(filename)s:%(lineno)s [%(levelname)s]: %(message)s
log_cli_date_format = %Y-%m-%d %H:%M:%S
于是可以看到日志格式
test_log.py::test_answer
--------------------------live log call ------------------------
2022-12-07 14:27:29 test_log.py:20 [INFO]: test case --------
2022-12-07 14:27:29 test_log.py:14 [INFO]: function inc--------
PASSED
保存日志文件
log_cli
的几个配置参数是设置控制台输出日志的等级和格式,如果我们想保存日志文件,需添加以下配置
(log_file
相关的结果是保存日志文件到本地)
[pytest]
log_cli = true
log_cli_level = info
log_cli_format = %(asctime)s %(filename)s:%(lineno)s [%(levelname)s]: %(message)s
log_cli_date_format = %Y-%m-%d %H:%M:%S
log_file = ./yoyo.log
log_file_level = debug
log_file_format = %(asctime)s %(filename)s:%(lineno)s [%(levelname)s]: %(message)s
log_file_date_format = %Y-%m-%d %H:%M:%S
log_file 参数设置保存日志文件为yoyo.log,log_file_level 设置日志等级为DEBUG,于是在yoyo.log文件会看到以下日志内容
2022-12-07 14:29:40 test_log.py:10 [DEBUG]: debug message: first to do .........
2022-12-07 14:29:40 test_log.py:19 [DEBUG]: run case: test_answer
2022-12-07 14:29:40 test_log.py:20 [INFO]: test case --------
2022-12-07 14:29:40 test_log.py:14 [INFO]: function inc--------
命令行参数配置
log日志的配置也可以用命令行参数配置(pytest -h可以查看)
--no-print-logs disable printing caught logs on failed tests.
--log-level=LOG_LEVEL logging level used by the logging module
--log-format=LOG_FORMAT log format as used by the logging module.
--log-date-format=LOG_DATE_FORMAT log date format as used by the logging module.
--log-cli-level=LOG_CLI_LEVEL cli logging level.
--log-cli-format=LOG_CLI_FORMAT log format as used by the logging module.
--log-cli-date-format=LOG_CLI_DATE_FORMAT log date format as used by the logging module.
--log-file=LOG_FILE path to a file when logging will be written to.
--log-file-level=LOG_FILE_LEVEL log file logging level.
--log-file-format=LOG_FILE_FORMAT log format as used by the logging module.
--log-file-date-format=LOG_FILE_DATE_FORMAT log date format as used by the logging module.
还可以使用pytest -o方式重写(即覆盖ini文件中的log相关的命令行参数)
pytest test_log.py -o log_cli=true -o log_cli_level=INFO
网易云完整视频课程《pytest+yaml 框架使用与开发》https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
报名咨询wx:283340479 (已报名的同学学习过程中有问题,都可以协助解决)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-12-07 pytest文档68-pytest-lazy-fixture 插件解决 pytest.mark.parametrize 中使用 fixture 问题
2020-12-07 pytest文档67-pytest.mark.parametrize 中使用 fixture
2018-12-07 关于面试总结4-python笔试题