This rule raises an issue when a @pytest.fixture function contains more than one yield statement.

Why is this an issue?

Pytest yield fixtures separate setup from teardown with a single yield statement. Code before yield runs before the test, the yielded value is passed to the test, and code after yield runs during teardown.

When a fixture contains more than one yield, pytest accepts the fixture during test setup and runs the test, but raises an error during teardown: Failed: fixture function has more than one 'yield'. Any code after the second yield, including cleanup such as closing connections, is never executed, which can leave resources open and break test isolation.

This rule targets @pytest.fixture setup and teardown patterns only. Ordinary generator functions that use multiple yield values outside fixture definitions are out of scope.

What is the potential impact?

Tests can satisfy their assertions yet still fail during teardown, producing confusing pass-then-fail diagnostics in CI until the fixture is corrected.

How to fix it

Remove the extra yield statements and keep only one. If you need to return multiple values, yield a tuple or other collection. If setup differs by condition, compute the value first and yield once on a single code path so teardown always runs afterward.

Use return instead of yield when the fixture does not need teardown logic.

Code examples

Noncompliant code example

@pytest.fixture
def database_connection():
    db = create_connection()
    yield db  # Noncompliant
    # Pytest raises "fixture function has more than one 'yield'" at teardown
    yield db
    db.close()

Compliant solution

@pytest.fixture
def database_connection():
    db = create_connection()
    yield db
    db.close()

Resources

Documentation