python config_error ai_generated true

fixture 'db_session' not found

ID: python/fixturenotfounderror-fixture-not-found

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-07-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active
3.10 active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

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

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

  2. Using fixture from conftest without importing 50% fail

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