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

【原文链接】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>

相关内容

热门资讯

古巴外长:古巴人民将抵御侵略捍... 转自:北京日报客户端古巴外交部长罗德里格斯6日表示,忠于国家斗争历史的古巴人民将抵御任何帝国主义侵略...
银杏送福 年味相传 (来源:湖州日报)转自:湖州日报  湖州市新风实验小学教育集团近日开展“银杏送福·年味相传”校园年货...
四部门联合发文推进职工文体工作... 近日,全国总工会、文化和旅游部、国家电影局、国家体育总局联合印发《关于深入推进新时代职工文体工作高质...
俄外交部声明:愿继续向委内瑞拉... 中新社莫斯科1月6日电 俄罗斯外交部6日发表声明,愿继续向委内瑞拉提供必要支持,并呼吁通过建设性对话...
沈阳市皇姑区举办惠民书画展 本报讯 记者朱柏玲报道 日前,“笔端生福运 墨韵贺新元”惠民书画展暨文化馆书法培训班结业交流活动在沈...