python config_error ai_generated true

日志输出未捕获:即使使用 -s 标志,捕获的日志也不会显示在测试报告中

Logging output not captured: Captured logs are not displayed in the test report even with -s flag

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

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

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

日志在根级别配置,没有 pytest 可以捕获的处理程序,或者未使用 caplog fixture。

English

Logging is configured at the root level without a handler that pytest can capture, or the caplog fixture is not used.

generic

解决方案

  1. 95% 成功率
    def test_logging(caplog):
        with caplog.at_level(logging.DEBUG):
            function_that_logs()
        assert 'Expected log' in caplog.text
  2. 90% 成功率
    [pytest]
    log_cli = true
    log_level = DEBUG

无效尝试

常见但无效的做法:

  1. 70% 失败

    This configures logging globally but may not integrate with pytest's capture mechanism.

  2. 80% 失败

    Print statements are captured, but logging is not, so the problem remains.