python runtime_error ai_generated true

AssertionError: Expected mock to have been called once. Called 0 times.

ID: python/unittest-mock-return-value-not-asserted

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

The mock object's call count doesn't match the expected number, often due to incorrect patch target or logic.

generic

中文

mock 对象的调用次数与预期不符,通常是由于补丁目标错误或逻辑错误。

Workarounds

  1. 90% success
    with patch('module.function') as mock_func:
        call_code()
        mock_func.assert_called_once()
  2. 85% success
    mock_func.assert_any_call(arg1, arg2)

Dead Ends

Common approaches that don't work:

  1. 70% fail

    This masks the real problem of why the code isn't calling the mock.

  2. 90% fail

    The test becomes ineffective.