python install_error ai_generated true

fixture 'mocker' not found available fixtures: cache, capfd, capsys, ...

ID: python/pytest-mock-not-installed

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-03-27First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.10 active
3.11 active
3.12 active

Root Cause

The `mocker` fixture is provided by pytest-mock, which is not installed or not in the active environment.

generic

中文

`mocker` fixture 由 pytest-mock 提供,但未安装或不在当前激活的环境中。

Workarounds

  1. 98% success
    pip install pytest-mock
    # or
    poetry add --group dev pytest-mock
    # or
    uv add --dev pytest-mock
  2. 90% success
    from unittest import mock
    
    def test_x():
        with mock.patch('myapp.views.requests.get') as m:
            m.return_value.json.return_value = {}
            myapp.views.fetch()

Dead Ends

Common approaches that don't work:

  1. 95% fail

    mock is a module, not a fixture; pytest tries to resolve it as a fixture and fails.

  2. 90% fail

    Returns the module, not a MagicMock controller; tests expecting `mocker.patch` fail with AttributeError.

  3. 90% fail

    pytest runs in the currently active env; installing elsewhere doesn't make the fixture available.