Pytest----caplog的应用场景以及使用方法
创始人
2024-03-20 17:28:01
0

【原文链接】Pytest----caplog的应用场景以及使用方法

文章目录

    • 如何在测试用例中设置日志级别
    • 如何对日志级别进行断言
    • 如何对日志内容进行断言
    • 如何同时对日志级别和日志内容进行断言

如何在测试用例中设置日志级别

通过caplog可以对特定的测试函数内设置日志级别,而不影响全局的日志级别,比如如下,首先在pytest.ini中开启实时日志。

[pytest]
log_cli = True

然后测试代码中test_demo和test_demo2中均打印debug、info、warning、error、critical级别的日志,不同的是在test_demo2中通过caplog临时将日志级别设置为debug级别,因为在默认情况下,日志级别为warning,因此根据理论分析,test_demo中的日志打印将采用默认的日志级别,即只会显示warning、error、critical级别的日志,而在test_demo2中由于临时将日志级别设置为debug,因此这里所有的日志都将显示处理。

import loggingdef test_demo():logging.debug("this is debug log ...")logging.info("this is info log ...")logging.warning("this is warning log ...")logging.error("this is error log ...")logging.critical("this is critical log ...")assert 1==1def test_demo2(caplog):caplog.set_level(logging.DEBUG)logging.debug("this is debug log ...")logging.info("this is info log ...")logging.warning("this is warning log ...")logging.error("this is error log ...")logging.critical("this is critical log ...")assert 1==1

执行结果如下,可以看出,在test_demo中只显示了warning、error、critical级别的日志,而在test_demo2中则所有的日志都显示出来了,即此时的日志级别已经通过caplog修改为debug级别了。

(demo-HCIhX0Hq) E:\demo>pytest
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 2 itemstest_demo.py::test_demo
---------------------- live log call ----------------------
WARNING  root:test_demo.py:6 this is warning log ...
ERROR    root:test_demo.py:7 this is error log ...
CRITICAL root:test_demo.py:8 this is critical log ...
PASSED                                               [ 50%]
test_demo.py::test_demo2
---------------------- live log call ----------------------
DEBUG    root:test_demo.py:13 this is debug log ...
INFO     root:test_demo.py:14 this is info log ...
WARNING  root:test_demo.py:15 this is warning log ...
ERROR    root:test_demo.py:16 this is error log ...
CRITICAL root:test_demo.py:17 this is critical log ...
PASSED                                               [100%]==================== 2 passed in 0.03s ====================(demo-HCIhX0Hq) E:\demo>

如何对日志级别进行断言

caplog会将日志都记录在records属性中,这样一来就可以在测试脚本末尾,对当前测试脚本中产生的日志级别进行判断,比如脚本中可能存在当某些条件触发时写入error日志,而在脚本末尾则可以对日志级别进行断言,比如要求日志不能有error日志,则可以使用类似如下测试代码。

import loggingdef test_demo(caplog):logging.warning("this is warning log ...")logging.error("this is error log ...")logging.critical("this is critical log ...")for record in caplog.records:assert record.levelname != "ERROR"

执行结果如下,

(demo-HCIhX0Hq) E:\demo>pytest
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 itemtest_demo.py::test_demo
---------------------- live log call ----------------------
WARNING  root:test_demo.py:4 this is warning log ...
ERROR    root:test_demo.py:5 this is error log ...
CRITICAL root:test_demo.py:6 this is critical log ...
FAILED                                               [100%]======================== FAILURES =========================
________________________ test_demo ________________________caplog = <_pytest.logging.LogCaptureFixture object at 0x0000029BEF29C608>def test_demo(caplog):logging.warning("this is warning log ...")logging.error("this is error log ...")logging.critical("this is critical log ...")for record in caplog.records:
>           assert record.levelname != "ERROR"
E           assert 'ERROR' != 'ERROR'
E            +  where 'ERROR' = .levelnametest_demo.py:8: AssertionError
-------------------- Captured log call --------------------
WARNING  root:test_demo.py:4 this is warning log ...
ERROR    root:test_demo.py:5 this is error log ...
CRITICAL root:test_demo.py:6 this is critical log ...
================= short test summary info =================
FAILED test_demo.py::test_demo - assert 'ERROR' != 'ERROR'
==================== 1 failed in 0.08s ====================(demo-HCIhX0Hq) E:\demo>

如何对日志内容进行断言

caplog同样可以做到对日志的内容进行断言,比如如下测试代码,判断日志中是否有 error log 内容。即通过record的message即可获得日志的内容。

import loggingdef test_demo(caplog):logging.warning("this is warning log ...")logging.error("this is error log ...")logging.critical("this is critical log ...")for record in caplog.records:assert "error log" not in record.meesge

执行结果如下,可以看到断言错误,即日志中含有 error log 内容。

(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 itemtest_demo.py::test_demo
---------------------- live log call ----------------------
WARNING  root:test_demo.py:4 this is warning log ...
ERROR    root:test_demo.py:5 this is error log ...
CRITICAL root:test_demo.py:6 this is critical log ...
FAILED======================== FAILURES =========================
________________________ test_demo ________________________caplog = <_pytest.logging.LogCaptureFixture object at 0x00000260C060D6C8>def test_demo(caplog):logging.warning("this is warning log ...")logging.error("this is error log ...")logging.critical("this is critical log ...")for record in caplog.records:
>           assert "error log" not in record.meesge
E           AttributeError: 'LogRecord' object has no attribute 'meesge'test_demo.py:8: AttributeError
-------------------- Captured log call --------------------
WARNING  root:test_demo.py:4 this is warning log ...
ERROR    root:test_demo.py:5 this is error log ...
CRITICAL root:test_demo.py:6 this is critical log ...
================= short test summary info =================
FAILED test_demo.py::test_demo - AttributeError: 'LogRecord' object has no attribute 'me...
==================== 1 failed in 0.08s ====================(demo-HCIhX0Hq) E:\demo>

如何同时对日志级别和日志内容进行断言

除此以外,caplog还可以对logger,日志级别,日志内容组成的元组进行判决,caplog.record_tuples 保存了所有logger、日志级别、日志内容组成的元组集合。
如下测试代码,这里有一点需要注意的是,当直接使用logging.xxx打印日志的时候,实际上使用的logger是root,这一点需要注意,若对python中logging有更深入的理解,比如定义自己的logger,则在此处就会很容易理解了。

import loggingdef test_demo(caplog):logging.warning("this is warning log ...")logging.error("this is error log ...")logging.critical("this is critical log ...")assert ("root",logging.ERROR,"this is error log ...") in caplog.record_tuples

执行结果如下,可以看到此时断言成功。

(demo-HCIhX0Hq) E:\demo>pytest
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 itemtest_demo.py::test_demo
---------------------- live log call ----------------------
WARNING  root:test_demo.py:4 this is warning log ...
ERROR    root:test_demo.py:5 this is error log ...
CRITICAL root:test_demo.py:6 this is critical log ...
PASSED                                               [100%]==================== 1 passed in 0.02s ====================(demo-HCIhX0Hq) E:\demo>

相关内容

热门资讯

笔试强训48天——day29 文章目录一. 单选1. 某台微机安装的是 64 位操作系统,“ 64 位”指的是&#x...
PyTorch初学者指南:数据... PyTorch初学者指南:数据操作 文章目录PyTorch初学者指南:数...
基于Android的班级圈系统... 需求信息: 1.前台安卓app,后台web管理 2.用户由教师家长学生...
【MySQL Shell】9.... 配置实例后,通过完成以下步骤创建 InnoDB ReplicaSet: ...
【深度学习笔记】手动推导反向传... 问题 现在才意识到,卷积神经网络在不同的层上的反向传播的计算公式不一样,之前一直按照全连接层的那种简...
实战五十六:基于机器学习的上海... 本文主要分析影响房价的因素,数据来源为链家网,机器学习模型的使用中,采用了三种线性模型,一种非线性模...
如何修复u盘损坏的文件?   由于U盘经常会在各种不同的电脑上使用,这让它很容易出现一些问题,如u...
排序算法_ 一、选择排序题目arr[0~N-1]范围上,找到最小值所在的位置...
监控圈子行业黑话 文章目录监控圈子行业黑话监控监控系统监控指标全局唯一字符串作为指标标识标签集的组合作为指标标识优雅高...
C++高精度运算(加减乘除) C++中在int 是32位bit是2^32-1。 C/C++基础数字...