python data_error ai_generated true

AssertionError: assert 'expected log' in ''

ID: python/pytest-caplog-no-output

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

版本兼容性

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

根因分析

logger 的级别高于消息级别,或 logging 配置了 propagate=False,导致 caplog 无法捕获记录。caplog 默认级别为 WARNING。

English

The logger's level is higher than the message level, or logging was configured with propagate=False, so caplog does not capture the record. caplog defaults to WARNING.

generic

解决方案

  1. 93% 成功率
    def test_logs(caplog):
        caplog.set_level(logging.INFO, logger='app')
        do_work()
        assert 'expected log' in caplog.text
    
    # app/logger.py
    logger = logging.getLogger('app')
    logger.propagate = True
  2. 90% 成功率
    def test_logs(caplog):
        caplog.set_level(logging.INFO)
        do_work()
        assert any(r.message == 'expected log' for r in caplog.records)

无效尝试

常见但无效的做法:

  1. 70% 失败

    -s disables output capture, which actually makes caplog less likely to see logs from some handlers.

  2. 65% 失败

    May add a handler but caplog still relies on propagation and its own level; often ineffective.