python type_error ai_generated true

断言错误:断言 <Mock name='mock()' id='140735234567890'> == 5

AssertionError: assert <Mock name='mock()' id='140735234567890'> == 5

ID: python/pytest-assert-repr-failure

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

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active

根因分析

测试将模拟对象与值进行比较,可能是因为模拟对象未设置返回值,因此默认返回另一个模拟对象。

English

A test is comparing a mock object to a value, likely because the mock was not set up to return a value, so it returns another mock by default.

generic

解决方案

  1. 95% 成功率 Set the mock's return_value to the expected integer value
    mock = MagicMock(return_value=5)
    result = mock()
    assert result == 5
    Or: mock.return_value = 5
  2. 85% 成功率 Ensure the code under test is calling the mock correctly
    Debug to see if the mock is being called. If not, the test may be comparing the mock object itself instead of its return value.

无效尝试

常见但无效的做法:

  1. Comparing the mock to itself using `is` 70% 失败

    This does not test the actual behavior; it only checks identity.

  2. Setting return_value to a string like '5' 50% 失败

    This changes the type; the test expects an integer 5.