Pytest----如何打开或关闭实时日志和捕获日志

【原文链接】Pytest----如何打开或关闭实时日志和捕获日志

Pytest中默认情况下实时日志是关闭的,捕获日志是打开的,因此这里主要介绍如何打开实时日志以及关闭捕获日志。
比如如下脚本:

import logging

def test_demo():
    logging.warning("this is warning log ...")
    logging.error("this is error log ...")
    logging.critical("this is critical log ...")
    assert 1==1

开启实时日志有两种方式,一种是通过命令行,pytest –o log_cli=true,如下所示:

(demo-HCIhX0Hq) E:\demo>pytest -o log_cli=true
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo
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>

在命令行中打开实时日志是比较繁琐的,因此另外一种方式就是在pytest.ini文件中配置。创建pytest.ini文件,然后写入如下配置即可打开实时日志。

[pytest]
log_cli = True

此时直接使用pytest命令即可显示实时日志了。如下所示。

(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>

当不设置日志级别时,默认情况下也是warning,即warning及以上的级别日志才会显示,info和debug日志在默认情况下是不会显示的,比如在测试脚本中增加info和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

执行结果如下,可以看到此时info和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 1 item

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                                               [100%]

==================== 1 passed in 0.02s ====================

(demo-HCIhX0Hq) E:\demo>

在pytest.ini中开启实时日志的同时,可以同时将日志级别以及日志格式同步设置一下,不如pytest.ini设置如下配置。

[pytest]
log_cli = True
log_cli_level = debug
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_date_format = %Y-%m-%d %H:%M:%S

此时再次执行,即debug和info日志此时均显示出来了,而且每行日志同时带有时间戳、日志级别、日志内容了。

(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 ----------------------
2022-12-05 16:22:17 DEBUG this is debug log ...
2022-12-05 16:22:17 INFO this is info log ...
2022-12-05 16:22:17 WARNING this is warning log ...
2022-12-05 16:22:17 ERROR this is error log ...
2022-12-05 16:22:17 CRITICAL this is critical log ...
PASSED                                               [100%]

==================== 1 passed in 0.02s ====================

(demo-HCIhX0Hq) E:\demo>

捕获日志默认情况下是开启的,即当用例失败时就会自动显示捕获的日志了,比如如下将测试用例修改为失败。

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==2

执行结果如下,即用例失败后显示了Captured log call即捕获日志。

(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 ----------------------
2022-12-05 16:28:59 DEBUG this is debug log ...
2022-12-05 16:28:59 INFO this is info log ...
2022-12-05 16:28:59 WARNING this is warning log ...
2022-12-05 16:28:59 ERROR this is error log ...
2022-12-05 16:28:59 CRITICAL this is critical log ...
FAILED                                               [100%]

======================== FAILURES =========================
________________________ test_demo ________________________

    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==2
E       assert 1 == 2

test_demo.py:9: AssertionError
-------------------- Captured log call --------------------
DEBUG    root:test_demo.py:4 this is debug log ...
INFO     root:test_demo.py:5 this is info log ...
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 1 == 2
==================== 1 failed in 0.08s ====================

(demo-HCIhX0Hq) E:\demo>

捕获日志pytest默认是打开的,这里顺便也提一下,对于捕获日志,也是可以通过在pytest.ini中配置日志级别、时间戳、格式等的,而且捕获日志配置与实时日志配置关键字是不一样的,需要注意分开,如下为配置捕获日志格式的。

[pytest]
log_level = debug
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S
	执行结果如下,可以看到此时捕获日志已经将debug和info级别的日志显示出来了,并且也带有时间戳和日志级别的信息。
(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 F                                       [100%]

======================== FAILURES =========================
________________________ test_demo ________________________

    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==2
E       assert 1 == 2

test_demo.py:9: AssertionError
-------------------- Captured log call --------------------
2022-12-05 17:12:33 DEBUG this is debug log ...
2022-12-05 17:12:33 INFO this is info log ...
2022-12-05 17:12:33 WARNING this is warning log ...
2022-12-05 17:12:33 ERROR this is error log ...
2022-12-05 17:12:33 CRITICAL this is critical log ...
================= short test summary info =================
FAILED test_demo.py::test_demo - assert 1 == 2
==================== 1 failed in 0.08s ====================

(demo-HCIhX0Hq) E:\demo>

而若想关闭捕获日志,则只需要使用–show-capture=no 参数即可,比如如下,可以看到,此时即没有捕获日志显示了,但是在实际应用中,一般不需要将捕获日志关闭,因为在默认用例通过时捕获日志就是不显示的,而当用例失败时,此时正是需要查看捕获日志的,因此对于捕获日志一般保持默认打开即可,需要做的就是去设置一下日志打印的格式等。

(demo-HCIhX0Hq) E:\demo>pytest --show-capture=no
=================== 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 F                                       [100%]

======================== FAILURES =========================
________________________ test_demo ________________________

    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==2
E       assert 1 == 2

test_demo.py:9: AssertionError
================= short test summary info =================
FAILED test_demo.py::test_demo - assert 1 == 2
==================== 1 failed in 0.07s ====================

(demo-HCIhX0Hq) E:\demo>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

redrose2100

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

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

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

打赏作者

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

抵扣说明:

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

余额充值