python runtime_error ai_generated true

失败:在 teardown 阶段找不到夹具 'client'

Failed: fixture 'client' not found in teardown

ID: python/pytest-fixure-teardown-error

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

版本兼容性

版本状态引入弃用备注
7.4.0 active

根因分析

在 fixture 的 teardown 代码中尝试访问另一个不存在的 fixture

English

在 fixture 的 teardown 代码中尝试访问另一个不存在的 fixture

generic

解决方案

  1. 90% 成功率
    @pytest.fixture
    def client():
        yield create_client()
        # teardown code
  2. 80% 成功率
    def client(request):
        c = create_client()
        request.addfinalizer(c.close)
        return c

无效尝试

常见但无效的做法:

  1. 80% 失败

    teardown 阶段不应创建新 fixture

  2. 60% 失败

    会导致资源泄漏