python install_error ai_generated true

fixture 'mocker' 未找到 可用 fixtures: cache, capfd, capsys, ...

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

ID: python/pytest-mock-not-installed

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-03-27首次发现

版本兼容性

版本状态引入弃用备注
3.10 active
3.11 active
3.12 active

根因分析

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

English

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

generic

解决方案

  1. 98% 成功率
    pip install pytest-mock
    # or
    poetry add --group dev pytest-mock
    # or
    uv add --dev pytest-mock
  2. 90% 成功率
    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()

无效尝试

常见但无效的做法:

  1. 95% 失败

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

  2. 90% 失败

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

  3. 90% 失败

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