python type_error ai_generated true

AssertionError: assert <Mock id='140123456789'> == 5

ID: python/pytest-assertion-rewrite-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-04-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.4.0 active

Root Cause

在断言中使用了 mock 对象而非实际返回值,导致比较失败。

generic

中文

在断言中使用了 mock 对象而非实际返回值,导致比较失败。

Workarounds

  1. 100% success
    `mock_obj.return_value = 5` 然后断言 `mock_obj() == 5`。
  2. 90% success
    `mock_obj.assert_called_once_with(参数)` 验证调用参数。

Dead Ends

Common approaches that don't work:

  1. 100% fail

    mock 对象始终不等于任何值,除非配置了 return_value。

  2. 50% fail

    如果调用参数不匹配,仍会失败,且不能验证返回值。