python runtime_error ai_generated true

日志捕获错误:不能同时使用 capsys 和 caplog

LogCaptureError: cannot use capsys and caplog at the same time

ID: python/pytest-capture-log-error

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

版本兼容性

版本状态引入弃用备注
7.4.0 active

根因分析

在同一个测试函数中同时请求了 capsys 和 caplog fixture,pytest 不支持

English

在同一个测试函数中同时请求了 capsys 和 caplog fixture,pytest 不支持

generic

解决方案

  1. 90% 成功率
    def test_foo(caplog): ... 使用 caplog.text 检查日志
  2. 85% 成功率
    def test_foo(capsys): ... 使用 capsys.readouterr()

无效尝试

常见但无效的做法:

  1. 50% 失败

    可能失去需要的功能

  2. 70% 失败

    capsys 捕获 stdout,不捕获日志