测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----caplog的应用场景以及使用方法
收藏本文
作者:redrose2100 类别: 日期:2022-12-05 16:30:17 阅读:990 次 消耗积分:0 分
[TOC] ![](https://redrose2100.oss-cn-hangzhou.aliyuncs.com/img/7cd47362-951c-11ee-986a-0242ac110004.png) ## 如何在测试用例中设置日志级别 通过caplog可以对特定的测试函数内设置日志级别,而不影响全局的日志级别,比如如下,首先在pytest.ini中开启实时日志。 ```bash [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,因此这里所有的日志都将显示处理。 ```python import logging def 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==1 def 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级别了。 ```python (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 items test_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日志,则可以使用类似如下测试代码。 ```python import logging 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" ``` 执行结果如下, ```bash (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 item test_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' =
.levelname test_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即可获得日志的内容。 ```python import logging 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 ``` 执行结果如下,可以看到断言错误,即日志中含有 error log 内容。 ```bash (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 item test_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,则在此处就会很容易理解了。 ```python import logging def 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 ``` 执行结果如下,可以看到此时断言成功。 ```bash (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 item test_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> ``` # 测试用例中如何获取setup中的日志 在有些测试场景下,希望测试步骤中对setup步骤中的日志进行一定的处理,比如判断setup的日志中是否有ERROR级别的日志,此时caplog可以提供这样的功能。 比如如下测试代码,即同过function_fixture中在关键字yield之前写了几条日志信息,,然后在测试脚本中可以对setup中的日志进行判断。 ```python import logging import pytest @pytest.fixture(scope="function",autouse=True) def function_fixture(): logging.warning("this is warning log ...") logging.error("this is error log ...") logging.critical("this is critical log ...") yield def test_demo(caplog): setup_records = caplog.get_records("setup") for record in setup_records: assert record.levelname != "ERROR" ``` 执行结果如下,可以看到此时已经拿到了setup中的日子,并且断言失败。 ```bash (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 item test_demo.py::test_demo --------------------- live log setup ---------------------- 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 ... FAILED [100%] ======================== FAILURES ========================= ________________________ test_demo ________________________ caplog = <_pytest.logging.LogCaptureFixture object at 0x00000239EF30D188> def test_demo(caplog): setup_records = caplog.get_records("setup") for record in setup_records: > assert record.levelname != "ERROR" E assert 'ERROR' != 'ERROR' E + where 'ERROR' =
.levelname test_demo.py:14: AssertionError ------------------- Captured log setup -------------------- 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 ... ================= short test summary info ================= FAILED test_demo.py::test_demo - assert 'ERROR' != 'ERROR' ==================== 1 failed in 0.08s ==================== (demo-HCIhX0Hq) E:\demo> ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/481
上一篇:
Pytest----如何打开或关闭实时日志和捕获日志
下一篇:
Pytest----如何正确的配置使用日志功能
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件