python runtime_error ai_generated true

Failed: fixture 'client' not found in teardown

ID: python/pytest-fixure-teardown-error

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.4.0 active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 80% fail

    teardown 阶段不应创建新 fixture

  2. 60% fail

    会导致资源泄漏