python runtime_error ai_generated true

AssertionError: Expected 'send_email' to be called once. Called 0 times.

ID: python/pytest-mock-reset-between-calls

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.10 active — — —
3.12 active — — —

Root Cause

The test asserts `mock.assert_called_once()` but the mock was reset (e.g. by `mock.reset_mock()` or a fixture that resets it) before the assertion, or the code under test was patched at the wrong target.

generic

中文

测试断言 `mock.assert_called_once()`,但 mock 在断言前被重置(例如被 `mock.reset_mock()` 或某个 fixture 重置),或者被测代码 patch 了错误的目标。

Workarounds

  1. 95% success
    Patch where the name is looked up and verify the patch target:
    
    @patch('app.services.user.send_email')
    def test_signup(mock_send):
        signup()
        mock_send.assert_called_once()
  2. 90% success
    Remove any `reset_mock()` calls between the action and the assertion, or use `mock.call_count` to inspect at the exact point.

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Still fails if the mock was never called; masks the real issue.

  2. 95% fail

    Repeating the assertion does not change the call count.