python config_error ai_generated true

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

fixture 'db_session' not found available fixtures: cache, capfd, ...

ID: python/pytest-fixture-not-found-typo

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

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

Fixture 名称拼写错误,定义在测试目录范围之外的 conftest.py 中,或者因插件未安装而未导入。

English

Fixture name is misspelled, defined in a conftest.py outside the test's directory scope, or not imported because the plugin is not installed.

generic

解决方案

  1. 95% 成功率
    Create `tests/conftest.py` with `@pytest.fixture
    def db_session(): ...` and ensure tests are under `tests/`.
  2. 90% 成功率
    `pip install pytest-django` then in pytest.ini: `[pytest]
    DJANGO_SETTINGS_MODULE = myproj.settings`

无效尝试

常见但无效的做法:

  1. 40% 失败

    Duplicates fixture logic across many test files and defeats the purpose of shared fixtures.

  2. 90% 失败

    Shadows a built-in pytest fixture and causes unexpected behavior in other tests.