python config_error ai_generated true

fixture 'db_session' 未找到

fixture 'db_session' not found

ID: python/fixturenotfounderror-fixture-not-found

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

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active
3.10 active

根因分析

Pytest 无法找到 fixture,因为它未定义、未导入或在测试函数中拼写错误。

English

Pytest cannot locate a fixture because it is not defined, imported, or spelled incorrectly in the test function.

generic

解决方案

  1. 95% 成功率 Define fixture in conftest.py at the appropriate level
    # conftest.py
    @pytest.fixture
    def db_session():
        return create_session()
  2. 90% 成功率 Use @pytest.fixture decorator in test file
    @pytest.fixture
    def db_session():
        return create_session()

无效尝试

常见但无效的做法:

  1. Defining fixture in the same file but with wrong name 70% 失败

    Fixture name must match exactly; typos cause not found errors.

  2. Using fixture from conftest without importing 50% 失败

    conftest fixtures are automatically available, but only if conftest is in the correct directory.