pytest文档81 - 如何管理Captured logging日志

前言

pytest 自动捕获级别为 WARNING 或以上的日志消息,并以与捕获的 stdout 和 stderr 相同的方式在每个失败测试的各自部分中显示它们。

日志显示

当输入pytest命令,不带任何参数时

pytest

如果运行的用例没有报错,那么打印的内容很简洁

以下示例,当用例中有报错的时候

# content of test_sample.py
import pytest
import logging
log = logging.getLogger(__name__)


@pytest.fixture
def first():
    print('set up :first to do ...')


def inc(x):
    log.warning('function inc--------')
    return x + 1


def test_answer(first):
    assert inc(3) == 6


运行后日志

collected 1 item                                                                                           

case\test_sample.py F                                                                                [100%]

================================================ FAILURES =================================================
_______________________________________________ test_answer _______________________________________________

first = None

    def test_answer(first):
>       assert inc(3) == 6
E       assert 4 == 6
E        +  where 4 = inc(3)

case\test_sample.py:18: AssertionError
------------------------------------------ Captured stdout setup ------------------------------------------
set up :first to do ...
-------------------------------------------- Captured log call --------------------------------------------
WARNING  case.test_sample:test_sample.py:13 function inc--------
========================================= short test summary info =========================================
FAILED case/test_sample.py::test_answer - assert 4 == 6
============================================ 1 failed in 0.24s ============================================


默认情况下,每个捕获的日志消息都显示模块、行号、日志级别和消息。
如果需要,可以通过传递特定的格式选项将日志和日期格式指定给日志模块支持的任何内容:

pytest --log-format="%(asctime)s %(levelname)s %(message)s" --log-date-format="%Y-%m-%d %H:%M:%S"

加上log的格式后显示效果

--------------------------- Captured stdout setup ----------------------------
set up :first to do ...
----------------------------- Captured log call ------------------------------
2022-11-16 11:30:17 WARNING function inc--------
========================== short test summary info ===========================
FAILED case/test_sample.py::test_answer - assert 4 == 6

--log-format--log-date-format 也可以加到pytest.ini的配置文件

[pytest]
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S

禁用捕获内容(stdout、stderr和log)

可以完全禁用对失败测试的捕获内容(stdout、stderr和log)的报告:

pytest --show-capture=no

禁用后就不再显示stdout、stderr和log

caplog 内置 fixture

在测试中,可以更改捕获的日志消息的日志级别。这由caplog fixture支撑:

def test_foo(caplog):
    caplog.set_level(logging.INFO)

默认情况下,级别在根记录器上设置,但为了方便起见,也可以设置任何记录器的日志级别

def test_foo(caplog):
    caplog.set_level(logging.CRITICAL, logger="root.baz")

测试结束时,将自动恢复设置的日志级别。

还可以使用上下文管理器临时更改with块内的日志级别:

def test_bar(caplog):
    with caplog.at_level(logging.INFO):
        pass

同样,默认情况下,根记录器的级别会受到影响,但任何记录器的级别都可以改为:

def test_bar(caplog):
    with caplog.at_level(logging.CRITICAL, logger="root.baz"):
        pass

最后,测试运行期间发送给记录器的所有日志都以两种日志的形式在设备上可用。LogRecord实例和最终日志文本。当您想断言消息内容时,这很有用:

def test_baz(caplog):
    func_under_test()
    for record in caplog.records:
        assert record.levelname != "CRITICAL"
    assert "wally" not in caplog.text

有关日志记录的所有可用属性,请参阅日志记录。LogRecord类。
如果您只想确保某些消息以给定的记录器名称记录,并具有给定的严重性和消息,您还可以使用record_tuples:

def test_foo(caplog):
    logging.getLogger().info("boo %s", "arg")

    assert caplog.record_tuples == [("root", logging.INFO, "boo arg")]

您可以调用caplog.clear()重置测试中捕获的日志记录:

def test_something_with_clearing_records(caplog):
    some_method_that_creates_log_records()
    caplog.clear()
    your_test_method()
    assert ["Foo"] == [rec.message for rec in caplog.records]

caplog.records 属性只包含当前阶段的记录,因此在setup阶段中,它只包含设置日志,与callteardown 阶段相同。

要从其他阶段访问日志,请使用caplog.get_records(when)方法。
例如,如果您希望确保使用某个固定装置的测试不会记录任何警告,您可以在拆卸过程中检查设置和调用阶段的记录,如下所示:

@pytest.fixture
def window(caplog):
    window = create_window()
    yield window
    for when in ("setup", "call"):
        messages = [
            x.message for x in caplog.get_records(when) if x.levelno == logging.WARNING
        ]
        if messages:
            pytest.fail(f"warning messages encountered during testing: {messages}")

完整的 API 参考 pytest.LogCaptureFixture https://docs.pytest.org/en/stable/reference/reference.html#pytest.LogCaptureFixture

实时日志

通过将log_cli配置选项设置为true,pytest将在日志记录直接发送到控制台时输出日志记录。

您可以通过传递--log-cli-level来指定日志记录的级别,对于该级别,具有相同或更高级别的日志记录将打印到控制台。
此设置接受日志记录文档中显示的日志记录级别名称或数值。此外,您还可以指定--log-cli-format--log-cli-date-format,如果没有提供,
这些格式将镜像并默认为--log-format--log-date-format,但仅应用于控制台日志处理程序。

还可以在配置INI文件中设置所有CLI日志选项。选项名称为:

log_cli_level

log_cli_format

log_cli_date_format

如果您需要记录对文件的整个测试套件日志调用,可以传递--log-file=/path/to/log/file
此日志文件以写模式打开,这意味着它将在每次运行测试会话时被覆盖。请注意,日志文件位置的相对路径(无论是在CLI上传递的还是在配置文件中声明的)始终相对于当前工作目录进行解析。

您还可以通过传递--logfile-level来指定日志文件的日志记录级别。此设置接受日志记录文档中显示的日志记录级别名称或数值。

此外,您还可以指定--log-file-format--log-file-date-format,它们等于--log-format--log-date-format ,但应用于日志文件日志处理程序。
也可以在配置INI文件中设置所有日志文件选项。选项名称为:

log_file

log_file_level

log_file_format

log_file_date_format

您可以调用set_log_path() t动态自定义log_file路径。该功能被认为是实验性的。

自定义颜色

如果启用彩色终端输出,则日志级别为彩色。通过add_color_level()支持更改默认颜色或在自定义日志级别上添加颜色。例子:

@pytest.hookimpl
def pytest_configure(config):
    logging_plugin = config.pluginmanager.get_plugin("logging-plugin")

    # Change color on existing log level
    logging_plugin.log_cli_handler.formatter.add_color_level(logging.INFO, "cyan")

    # Add color to a custom log level (a custom log level `SPAM` is already set up)
    logging_plugin.log_cli_handler.formatter.add_color_level(logging.SPAM, "blue")

Warning
This feature and its API are considered experimental and might change between releases without a deprecation notice.
警告
此特性及其API被认为是实验性的,可能会在不同版本之间进行更改,而不会发出弃用通知。

说明

这个特性是作为pytest-catchlog插件的替代品引入的,它们相互冲突。引入此功能时,与pytest-capturelog的向后兼容性API已被删除,因此如果出于此原因您仍然需要pytest-catchlog,您可以通过在pytest.ini中添加以下内容来禁用内部功能:

[pytest]
    addopts=-p no:logging

pytest 3.4中的不兼容更改

该功能在3.3中引入,在社区反馈后,在3.4中进行了一些不兼容的更改:

  • 除非Log_level配置或--log-level命令行选项明确请求,否则不再更改日志级别。这允许用户自己配置记录器对象。设置log_level将设置全局捕获的级别,因此如果特定测试需要的级别低于此级别,请使用caplog.set_level()功能,否则该测试将容易失败。
  • 实时日志现在在默认情况下被禁用,并且可以通过将log_cli 配置选项设置为true来启用。启用后,详细信息会增加,因此每个测试的日志记录都是可见的。
  • 实时日志现在被发送到sys.stdout ,不再需要-s命令行选项。

如果要部分恢复3.3版的日志记录行为,可以将以下选项添加到ini文件中:

[pytest]
log_cli=true
log_level=NOTSET

以上知识点来自官方文档https://docs.pytest.org/en/stable/how-to/logging.html
网易云完整视频课程《pytest+yaml 框架使用与开发》https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
报名咨询wx:283340479 (已报名的同学学习过程中有问题,都可以协助解决)

posted @ 2022-11-16 12:03  上海-悠悠  阅读(738)  评论(0编辑  收藏  举报