python runtime_error ai_generated true

LogCaptureError: cannot use capsys and caplog at the same time

ID: python/pytest-capture-log-error

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.4.0 active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 50% fail

    可能失去需要的功能

  2. 70% fail

    capsys 捕获 stdout,不捕获日志