python module_error ai_generated true

AttributeError: 'LogCaptureFixture' object has no attribute 'records'

ID: python/pytest-capture-caplog-attribute-error

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active

Root Cause

Using an outdated or incorrect API for caplog; 'records' was introduced in pytest 6.x but may not be available in older versions or when caplog is not properly initialized.

generic

中文

使用了过时或不正确的 caplog API;'records' 在 pytest 6.x 中引入,但在旧版本或 caplog 未正确初始化时可能不可用。

Workarounds

  1. 95% success Upgrade pytest to version 6.0 or later to support caplog.records.
    pip install pytest>=6.0
  2. 85% success Use caplog.record_tuples instead of caplog.records for backward compatibility.
    assert ('module', logging.INFO, 'message') in caplog.record_tuples

Dead Ends

Common approaches that don't work:

  1. Trying to access caplog.records without checking pytest version 70% fail

    If pytest version is below 6.0, 'records' attribute doesn't exist.

  2. Using caplog.text instead of caplog.records for log level checks 65% fail

    caplog.text returns a string, not log records, so level filtering fails.