python config_error ai_generated true

Log message not captured in caplog.records

ID: python/pytest-capture-logging-not-working

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.9 active

Root Cause

日志记录器未配置为使用 pytest 的捕获处理器,通常因为日志器名称错误或传播设置问题。

generic

中文

日志记录器未配置为使用 pytest 的捕获处理器,通常因为日志器名称错误或传播设置问题。

Workarounds

  1. 90% success 确保日志器名称正确
    logger = logging.getLogger('my_module')
    caplog.set_level(logging.INFO)
    logger.info('test')
  2. 80% success 配置文件配置
    在 conftest.py 中配置 logging 插件,确保传播开启

Dead Ends

Common approaches that don't work:

  1. 在测试内配置 basicConfig 60% fail

    尝试在测试中调用 logging.basicConfig,但 pytest 已经配置了处理器

  2. 错误地使用 __name__ 50% fail

    使用 logging.getLogger(__name__) 但测试模块名称与预期不符