python config_error ai_generated true

pytest.PytestConfigWarning: 在 'tests/' 中未找到 conftest.py

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

ID: python/pytest-conftest-not-found

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

版本兼容性

版本状态引入弃用备注
7.0.0 active
8.0.0 active

根因分析

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

English

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

解决方案

  1. 95% 成功率 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% 成功率 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.

无效尝试

常见但无效的做法:

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

    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% 失败

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