python runtime_error ai_generated true

AssertionError:assert 'expected message' in []

AssertionError: assert 'expected message' in []

ID: python/pytest-caplog-empty

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-07-19首次发现

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

caplog 未捕获任何记录,因为日志传播被禁用、日志记录器级别过滤了消息,或者日志记录调用发生在安装 caplog 之前(例如在导入时)。

English

caplog captured no records because logger propagation was disabled, the logger level filtered the message, or the logging call happened before caplog was installed (e.g., at import time).

generic

解决方案

  1. 95% 成功率
    def test_logs(caplog):
        import logging
        caplog.set_level(logging.INFO, logger="app")
        do_work()
        assert "expected message" in caplog.text
  2. 93% 成功率
    def test_logs(caplog):
        with caplog.at_level(logging.DEBUG, logger="app"):
            do_work()
        assert "expected message" in caplog.text
  3. 88% 成功率
    logger = logging.getLogger("app")
    logger.propagate = True  # do not disable in tests

无效尝试

常见但无效的做法:

  1. 90% 失败

    Level must be set before the log call to capture it.

  2. 80% 失败

    Defeats the purpose of testing logs and doesn't fix capture.

  3. 95% 失败

    clear() empties the buffer; it doesn't populate it.