即使我正在测试的功能正在记录日志,为什么caplog.text也为空? [英] Why is caplog.text empty, even though the function I'm testing is logging?

查看:68
本文介绍了即使我正在测试的功能正在记录日志,为什么caplog.text也为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pytest来测试我的函数是否正在记录预期的文本,例如已解决的此问题(等效的pyunit为 assertLogs ).遵循 pytest日志记录文档之后,我通过了caplog 固定到测试仪.该文档指出:

I'm trying to use pytest to test if my function is logging the expected text, such as addressed this question (the pyunit equivalent would be assertLogs). Following the pytest logging documentation, I am passing the caplog fixture to the tester. The documentation states:

在测试运行过程中,几乎所有发送到记录器的日志都可以通过Fixture.LogRecord实例和最终日志文本的形式在设备上使用.

Lastly all the logs sent to the logger during the test run are made available on the fixture in the form of both the logging.LogRecord instances and the final log text.

我正在测试的模块是:

import logging
logger = logging.getLogger(__name__)

def foo():
    logger.info("Quinoa")

测试人员是:

def test_foo(caplog):
    from mwe16 import foo
    foo()
    assert "Quinoa" in caplog.text

我希望该测试能够通过.但是,由于 caplog.text 为空,使用 pytest test_mwe16.py 运行测试会显示测试失败:

I would expect this test to pass. However, running the test with pytest test_mwe16.py shows a test failure due to caplog.text being empty:

============================= test session starts ==============================
platform linux -- Python 3.7.3, pytest-5.3.0, py-1.8.0, pluggy-0.13.0
rootdir: /tmp
plugins: mock-1.12.1, cov-2.8.1
collected 1 item

test_mwe16.py F                                                          [100%]

=================================== FAILURES ===================================
___________________________________ test_foo ___________________________________

caplog = <_pytest.logging.LogCaptureFixture object at 0x7fa86853e8d0>

    def test_foo(caplog):
        from mwe16 import foo
        foo()
>       assert "Quinoa" in caplog.text
E       AssertionError: assert 'Quinoa' in ''
E        +  where '' = <_pytest.logging.LogCaptureFixture object at 0x7fa86853e8d0>.text

test_mwe16.py:4: AssertionError
============================== 1 failed in 0.06s ===============================

尽管 foo()向记录器发送文本,为什么 caplog.text 为空?如何使用 pytest 来使 caplog.text 捕获记录的文本,或者以其他方式验证文本是否被记录?

Why is caplog.text empty despite foo() sending text to a logger? How do I use pytest such that caplog.text does capture the logged text, or otherwise verify that the text is being logged?

推荐答案

此处文档不清楚.经过反复试验,尽管在测试运行期间发送给记录器的所有日志均可用"文本,它仍仅捕获具有特定日志级别的日志.要实际上捕获所有日志,需要使用

The documentation is unclear here. From trial and error, and notwithstanding the "all the logs sent to the logger during the test run are made available" text, it still only captures logs with certain log levels. To actually capture all logs, one needs to set the log level for captured log messages using caplog.set_level or the caplog.at_level context manager, so that the test module becomes:

import logging
def test_foo(caplog):
    from mwe16 import foo
    with caplog.at_level(logging.DEBUG):
        foo()
    assert "Quinoa" in caplog.text

现在,测试通过:

============================= test session starts ==============================
platform linux -- Python 3.7.3, pytest-5.3.0, py-1.8.0, pluggy-0.13.0
rootdir: /tmp
plugins: mock-1.12.1, cov-2.8.1
collected 1 item

test_mwe16.py .                                                          [100%]

============================== 1 passed in 0.04s ===============================

这篇关于即使我正在测试的功能正在记录日志,为什么caplog.text也为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆