python runtime_error ai_generated true

AssertionError: assert <unittest.mock.MagicMock name='mock.method' id='140234567890'> is not None

ID: python/pytest-assertion-rewriting-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-08-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active
3.10 active
3.11 active
3.12 active

Root Cause

A test asserts that a mock object is not None, but the mock is returned instead of the actual value, indicating the mock was not configured correctly.

generic

中文

测试断言模拟对象不是 None,但返回了模拟对象而不是实际值,表明模拟对象配置不正确。

Workarounds

  1. 95% success
    Configure the mock to return a specific value: `mock.method.return_value = 'real_value'`
  2. 90% success
    Use autospec=True in patch to ensure the mock matches the real method's signature.

Dead Ends

Common approaches that don't work:

  1. 70% fail

    This may still return a mock if the attribute is not properly set up, leading to confusing behavior.

  2. 90% fail

    A mock object is truthy, so this assertion always passes, masking the real issue.