python runtime_error ai_generated true

AssertionError: assert 'expected message' in []

ID: python/pytest-caplog-empty

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-07-19First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active — — —
8.x active — — —

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 90% fail

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

  2. 80% fail

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

  3. 95% fail

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