python config_error ai_generated true

使用错误:@pytest.mark.usefixtures 使用了未知的夹具 'nonexistent_fixture'

UsageError: @pytest.mark.usefixtures with unknown fixture 'nonexistent_fixture'

ID: python/pytest-mark-usefixtures-error

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

版本兼容性

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

根因分析

在 @pytest.mark.usefixtures 中指定的夹具名称在任何 conftest.py 或测试文件中都不存在。

English

The fixture name specified in @pytest.mark.usefixtures does not exist in any conftest.py or test file.

generic

解决方案

  1. 95% 成功率 Define the missing fixture in the test file or conftest.py.
    @pytest.fixture
    def nonexistent_fixture(): return 'value'
  2. 90% 成功率 Correct the fixture name to match an existing fixture.
    @pytest.mark.usefixtures('existing_fixture')

无效尝试

常见但无效的做法:

  1. Adding a fixture with the same name but wrong scope 60% 失败

    The fixture must be defined and accessible; scope mismatch may still cause errors.

  2. Removing the usefixtures marker entirely 70% 失败

    If the fixture is required for test setup, tests may fail due to missing dependencies.