• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
redrose2100
帮助1

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjahig
系列文章
更多 icon
同类精品
更多 icon
继续加载