python runtime_error ai_generated true

Fixture 'db' called directly. Fixtures are not meant to be called directly

ID: python/pytest-fixture-return-not-yield

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2025-06-30First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.7 active

Root Cause

在测试中直接调用fixture函数,而不是作为参数注入,导致pytest报错。

generic

中文

测试中直接调用了fixture函数,而非通过参数注入。

Workarounds

  1. 100% success
    def test_example(db):
        assert db is not None
  2. 90% success
    def test_example(request):
        db = request.getfixturevalue('db')

Dead Ends

Common approaches that don't work:

  1. 80% fail

    失去fixture的依赖注入和清理功能。

  2. 90% fail

    直接调用返回的是fixture对象,不是预期值。