Pytest----caplog的应用场景以及使用方法

【原文链接】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 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级别了。

(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日志,则可以使用类似如下测试代码。

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"

执行结果如下,

(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' = <LogRecord: root, 40, E:\demo\test_demo.py, 5, "this is error log ...">.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即可获得日志的内容。

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 内容。

(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,则在此处就会很容易理解了。

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

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

(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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
pytest-xdist是pytest的一个插件,它可以用于在多个进程或者多个机器上并行执行pytest测试用例,从而加速测试运行速度。以下是pytest-xdist的具体使用方法: 1. 安装pytest-xdist插件:可以使用pip命令进行安装,命令如下: ``` pip install pytest-xdist ``` 2. 运行pytest测试用例:在运行pytest命令时,需要添加`-n`选项来指定要使用的进程或者机器数量,例如: ``` pytest -n 2 # 使用2个进程并行执行测试用例 ``` 3. 设置运行模式:pytest-xdist支持多种运行模式,包括load, loadscope, loadfile, loadfileglob和no. - load模式:在所有进程或机器上加载测试用例集,然后在每个进程或机器上执行它们。 - loadscope模式:在所有进程或机器上加载指定作用域的测试用例,然后在每个进程或机器上执行它们。 - loadfile模式:在所有进程或机器上加载指定的测试文件,然后在每个进程或机器上执行它们。 - loadfileglob模式:在所有进程或机器上加载符合指定glob表达式的测试文件,然后在每个进程或机器上执行它们。 - no模式:不使用xdist插件,单进程执行测试用例。 可以使用`-n`选项后面加上一个模式名称来指定运行模式,例如: ``` pytest -n 2 --dist=loadfile # 使用2个进程并行执行指定的测试文件 ``` 4. 设置分布式测试环境:如果要在多个机器上执行pytest测试用例,需要先设置好分布式测试环境。可以使用`-n`选项后面加上一个地址列表来指定分布式测试环境,例如: ``` pytest -n 2 --dist=load --tx tcp://192.168.1.10:8888 --tx tcp://192.168.1.11:8888 # 在两台机器上执行测试用例 ``` 其中,`--tx`选项后面跟的是一个TCP地址,可以是IP地址或者域名和端口号,用于指定要连接的机器和端口号。 5. 其他选项:除了上述选项外,pytest-xdist还支持其他选项,例如`--boxed`选项可以在每个进程或机器上使用独立的虚拟环境,`--max-worker-restart`选项可以指定最大重启次数等。 以上就是pytest-xdist的具体使用方法,通过并行执行测试用例,可以大大提高测试运行速度,特别是对于测试用例较多、运行时间较长的情况,效果更加明显。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

redrose2100

您的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值