SoFunction
Updated on 2024-11-12

Pytes are properly configured to use the logging feature

In pytest automated testing, if simply from the point of view of the application, it is possible not to understand the part of pytest to display information and the principle, can be completely through the use of recommended configurations, so that you can do a relatively general logging configuration.
Here we recommend using the following configuration, which log_cli related to the four configurations are used to configure live log that is, real-time logging, while the other three configurations are used to configure capture log that is, capture log. Respectively on their log level, log format, timestamp format settings, for example, here the log level are set to info, of course, if the script is stable after the submission of automated test code repository can be adjusted to the level of log warning.

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

log_level = info
log_format = %(asctime)s | %(levelname)s | %(filename)s:%(lineno)s | %(message)s"
log_date_format = %Y-%m-%d %H:%M:%S

Here is a simple test script to show the effect of the above logging configuration.

import logging

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

The results are as follows, you can see that here shows a real-time log (live log), while not showing debug level logs, capture log (capture log) also does not show debug level logs, and timestamps and log formats are relatively more in line with the actual application, so here in the recommended configuration of logs, it can be taken directly to the use.

(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: 
plugins: assume-2.4.3, rerunfailures-10.2
collected 1 item

test_demo.py::test_demo
---------------------- live log call ----------------------
2022-12-06 00:42:06 | INFO | test_demo.py:5 | this is info log ..."
2022-12-06 00:42:06 | WARNING | test_demo.py:6 | this is warning log ..."
2022-12-06 00:42:06 | ERROR | test_demo.py:7 | this is error log ..."
2022-12-06 00:42:06 | CRITICAL | test_demo.py:8 | this is critical log ..."
FAILED                                               [100%]

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

    def test_demo():
        ("this is debug log ...")
        ("this is info log ...")
        ("this is warning log ...")
        ("this is error log ...")
        ("this is critical log ...")
>       assert 1==2
E       assert 1 == 2

test_demo.py:9: AssertionError
-------------------- Captured log call --------------------
2022-12-06 00:42:06 | INFO | test_demo.py:5 | this is info log ..."
2022-12-06 00:42:06 | WARNING | test_demo.py:6 | this is warning log ..."
2022-12-06 00:42:06 | ERROR | test_demo.py:7 | this is error log ..."
2022-12-06 00:42:06 | CRITICAL | 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.07s ====================

(demo-HCIhX0Hq) E:\demo>

To this point this article on the correct configuration of the use of Pytes logging function of the article is introduced to this, more related Pytes logging configuration content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!