python config_error ai_generated true

AssertionError: assert 'user created' in '' + where '' = caplog.text

ID: python/pytest-caplog-no-records

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

Version Compatibility

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

Root Cause

caplog captures records via the root logger handler, but the application logger has `propagate = False`, a custom handler with its own level, or the log level is higher than the emitted message.

generic

中文

caplog 通过根 logger 处理器捕获记录,但应用 logger 设置了 `propagate = False`、自定义处理器及级别,或日志级别高于所发出的消息。

Workarounds

  1. 90% success
    Force propagation and set level on the specific logger:
    def test_create(caplog):
        caplog.set_level(logging.INFO, logger='myapp.users')
        create_user('alice')
        assert 'user created' in caplog.text
  2. 82% success
    If propagate is intentionally False, attach caplog's handler:
    logger = logging.getLogger('myapp.users')
    logger.addHandler(caplog.handler)
    caplog.set_level(logging.INFO)

Dead Ends

Common approaches that don't work:

  1. 70% fail

    If propagate=False, records never reach caplog's handler regardless of level.

  2. 85% fail

    logger.info returns None; printing does not capture anything and the assertion still fails.