pytest mock.patch not working (tested fixes)

If mock.patch has no effect, you patched the wrong name: after from requests import get, patch "yourmodule.get", not "requests.get". fixture 'mocker' not found means pytest-mock is not installed in the environment running pytest.

Most "mock not working" bugs in pytest are one of four things: patching the wrong name, the mocker fixture missing, a typo in the target or assertion, or a call that differs from what you assert. Measured on pytest 9.1.1, pytest-mock 3.17.0, requests 2.34.2, Python 3.13.5.

1. Patch where the name is looked up, not where it is defined

$ cat weather.py
import requests
from requests import get

def temp_module(city):
    return requests.get(f"https://api.example/{city}").json()["t"]

def temp_from(city):
    return get(f"https://api.example/{city}").json()["t"]
$ cat test_weather.py
from unittest import mock
import weather

def test_patch_library():                       # patches requests.get
    with mock.patch("requests.get") as g:
        g.return_value.json.return_value = {"t": 21}
        assert weather.temp_module("oslo") == 21
        assert weather.temp_from("oslo") == 21   # 'from requests import get' kept the real one

def test_patch_where_used():                    # patches the name weather looks up
    with mock.patch("weather.get") as g:
        g.return_value.json.return_value = {"t": 21}
        assert weather.temp_from("oslo") == 21
$ pytest -q --tb=line | tail -2
FAILED test_weather.py::test_patch_library - requests.exceptions.ConnectionEr...
1 failed, 1 passed in 0.08s

mock.patch("requests.get") replaces the attribute on the requests module. temp_module looks up requests.get at call time, so it sees the mock. temp_from did from requests import get, which bound the real function into weather at import time, so the test made a real HTTP request (here it failed to resolve the host). Patch "weather.get", the module that uses it.

2. fixture 'mocker' not found

$ cat test_m.py
def test_m(mocker):
    mocker.patch("os.getcwd", return_value="/x")
$ pytest -q -p no:pytest_mock | grep -E "not found|passed|failed|error"
E       fixture 'mocker' not found
1 error in 0.00s
$ pytest -q | tail -1
1 passed in 0.00s

mocker comes from the pytest-mock plugin, not pytest itself. -p no:pytest_mock above disables the installed plugin to show what you get when it is missing. Install pytest-mock into the same environment that runs pytest (python -m pip install pytest-mock), or use unittest.mock.patch directly.

3. AttributeError: does not have the attribute, not a valid assertion, autospec TypeError

$ cat test_typos.py
from unittest import mock
import json

def test_wrong_target():
    with mock.patch("json.lods"):
        pass

def test_typo_assert():
    m = mock.Mock()
    m.save(1)
    m.asert_called_once_with(2)

def test_autospec():
    with mock.patch("json.loads", autospec=True) as m:
        json.loads("{}", "extra", "args")
$ pytest -q | grep -E "^E  |passed|failed"
E           AttributeError: <module 'json' from '/usr/lib/python3.13/json/__init__.py'> does not have the attribute 'lods'
E               AttributeError: 'asert_called_once_with' is not a valid assertion. Use a spec for the mock if 'asert_called_once_with' is meant to be an attribute.. Did you mean: 'assert_called_once_with'?
E                       TypeError: too many positional arguments
3 failed in 0.17s

A misspelled patch target raises AttributeError ... does not have the attribute (add create=True only if the attribute really should not exist yet). A misspelled assertion method on a Mock raises instead of silently passing. With autospec=True the mock keeps the real signature, so a wrong call fails with TypeError instead of being accepted.

4. AssertionError: expected call not found

$ cat test_called.py
from unittest import mock

def test_called():
    m = mock.Mock()
    m.save("a.txt", mode="w")
    m.save.assert_called_once_with("a.txt", mode="wb")
$ pytest -q | grep -E "^E  |passed|failed" | head -4
E       AssertionError: expected call not found.
E       Expected: save('a.txt', mode='wb')
E         Actual: save('a.txt', mode='w')
E

The message prints Expected and Actual calls; positional and keyword arguments must match exactly. Use m.save.call_args or m.mock_calls to see what really happened, or mock.ANY for arguments you do not care about.

Related: pytest fixture not found, pytest error importing plugin, pytest caplog.

Frequently asked questions

Why is my mock.patch not working?

The code under test looks the name up somewhere else. Patch the name in the module that uses it (e.g. myapp.module.get), especially after 'from x import y'.

How do I fix fixture 'mocker' not found?

Install pytest-mock in the same environment that runs pytest: python -m pip install pytest-mock.

What does 'does not have the attribute' mean in mock.patch?

The patch target string names an attribute that does not exist on that module or class, usually a typo or the wrong module path.

Written with AI assistance. Measured on 2026-10-23 with pytest 9.1.1, pytest-mock 3.17.0 and requests 2.34.2 on Python 3.13.5 in a clean virtualenv. Each block was run verbatim and the output pasted unedited. PlainHeaders is our extension; Python and pytest are free.