Fixtures
Pytest · Reference cheat sheet
Fixtures
Pytest · Reference cheat sheet
📋 Overview
Fixtures provide setup/teardown via dependency injection. Tests request fixtures by parameter name; scopes control lifetime (function, class, module, package, session).
🔧 Core concepts
| Concept | Meaning |
|---|---|
@pytest.fixture | Define reusable resource |
yield | Setup before, teardown after |
scope | How often to recreate |
autouse | Apply without requesting |
params | Parametrized fixture |
| Fixture factory | Return a creator callable |
Prefer fixtures over setup/teardown methods for composability.
💡 Examples
Basic yield fixture:
import pytest
@pytest.fixture
def db():
conn = connect()
yield conn
conn.close()
def test_query(db):
assert db.execute("SELECT 1").fetchone()[0] == 1Scopes:
@pytest.fixture(scope="session")
def app():
return create_app()
@pytest.fixture(scope="module")
def client(app):
return app.test_client()Factory:
@pytest.fixture
def make_user(db):
created = []
def _make(**kwargs):
u = User(**kwargs)
db.add(u)
created.append(u)
return u
yield _make
for u in created:
db.delete(u)Autouse:
@pytest.fixture(autouse=True)
def _fast_cache(monkeypatch):
monkeypatch.setenv("CACHE_URL", "memory://")⚠️ Pitfalls
- Session-scoped mutable state leaking between tests.
- Requesting heavy fixtures unnecessarily — keep scopes tight.
- Using
returnwhen teardown is needed — useyield. - Circular fixture dependencies.
- Overusing
autouse— hard to see what a test depends on.