python config_error ai_generated true

pytest.PytestConfigWarning: conftest.py not found in 'tests/'

ID: python/pytest-conftest-not-found

Also available as: JSON · Markdown · 中文
80%Fix Rate
81%Confidence
0Evidence
2024-09-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.0.0 active
8.0.0 active

Root Cause

pytest expects a conftest.py file in the test directory for shared fixtures and hooks, but it is missing, which may cause fixture resolution failures if tests rely on conftest-defined fixtures.

generic

中文

pytest期望在测试目录中存在conftest.py文件以共享fixture和钩子,但该文件缺失,如果测试依赖于conftest定义的fixture,可能会导致fixture解析失败。

Workarounds

  1. 95% success Create a proper conftest.py file with the necessary fixtures and hooks
    In the tests/ directory, create conftest.py:
    import pytest
    
    @pytest.fixture
    def shared_fixture():
        return 'data'
    Then tests can use 'shared_fixture' without importing.
  2. 70% success Specify a custom conftest path using pytest's --confcutdir option
    pytest --confcutdir=tests/ tests/
    This tells pytest to look for conftest only in the tests/ directory.

Dead Ends

Common approaches that don't work:

  1. Creating an empty conftest.py file without any content 50% fail

    This suppresses the warning but does not provide the necessary fixtures, causing test failures later.

  2. Moving all fixtures to each individual test file 30% fail

    This duplicates code and reduces maintainability, but may resolve the immediate warning.