python config_error ai_generated true

AssertionError: assert 'expected message' in []

ID: python/pytest-caplog-not-capturing

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-06-30First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active
8.x active

Root Cause

The caplog fixture did not capture any log records because the logger's level is higher than the log message level, or the logger is not propagating to the root logger.

generic

中文

caplog 夹具未捕获任何日志记录,因为记录器的级别高于日志消息级别,或者记录器未传播到根记录器。

Workarounds

  1. 92% success
    def test_logging(caplog):
        caplog.set_level(logging.INFO)
        my_function()
        assert 'expected message' in caplog.text
  2. 88% success
    logger = logging.getLogger('myapp')
    logger.propagate = True
    # Or use caplog.set_level with the specific logger name

Dead Ends

Common approaches that don't work:

  1. 85% fail

    The level must be set before the log call. Setting it after has no effect on already-emitted records.

  2. 70% fail

    This bypasses the logging system entirely and does not test the actual logging behavior. It also clutters test output.