python
runtime_error
ai_generated
true
AssertionError: expected call not found. Expected: send(1) Actual: not called.
ID: python/pytest-mock-mocker-scope-mismatch
80%Fix Rate
83%Confidence
0Evidence
2024-07-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.14.x | active | — | — | — |
Root Cause
mocker.patch was applied inside a fixture with function scope, but the patched call happens in a session-scoped setup that ran before the patch was installed.
generic中文
mocker.patch 在函数作用域的 fixture 中应用,但被 patch 的调用发生在会话作用域 setup 中,该 setup 在 patch 安装之前已执行。
Workarounds
-
90% success
@pytest.fixture(scope='session') def patched_send(): with mock.patch('myapp.notify.send') as m: yield m def test_x(patched_send): run_setup() patched_send.assert_called_with(1) -
93% success
def test_x(mocker): m = mocker.patch('myapp.notify.send') run_setup() # called here, not in a fixture m.assert_called_with(1)
Dead Ends
Common approaches that don't work:
-
90% fail
The mock was never called in the first place; resetting does not retroactively capture calls that happened before patching.
-
85% fail
create=True only bypasses AttributeError; the timing problem (patch applied after the call) is unchanged.