A pytest fixture is a function decorated with @pytest.fixture that prepares something a test needs, such as data, a temporary file or a database connection. A test uses a fixture by naming it as a parameter, and pytest runs the fixture and passes in its return value. Fixtures replace the setUp and tearDown methods of xUnit-style frameworks with explicit, reusable dependencies.
This guide covers pytest fixtures on pytest 8 and Python 3.13: how to define and request them, how they combine, and which built-in fixtures you can use right away.
Tests often repeat the same setup: create an object, fill a list, open a connection. Copying that into every test hides what the test is really about and makes changes painful. A fixture moves the setup into one named place.
Without a fixture:
The setup is duplicated, and it grows with each new test.
Here is the same code with a fixture:
The parameter name filled_cart matches the fixture function name. This is dependency injection by name. By default a fixture runs once per test, so each test receives a fresh Cart and tests cannot affect each other.
A fixture can request another fixture in the same way a test does, which lets you build setup in layers:
pytest builds the dependency graph and creates each fixture once per test, even when several fixtures and the test itself request the same one. In the example above, a test that requests both empty_cart and filled_cart receives the same Cart object.
pytest ships with fixtures for common needs. These are the ones you will use most:
| Fixture | What it gives you |
|---|---|
tmp_path | A unique temporary directory as a pathlib.Path |
monkeypatch | Safely set or remove attributes, environment variables and dictionary items |
capsys | Captures text written to stdout and stderr |
caplog | Captures log records |
request | Information about the requesting test, used inside fixtures |
Changes made by monkeypatch are undone automatically after the test, and tmp_path directories are cleaned up by pytest according to its retention policy.
Two options change how fixtures are applied.
Pass params to run every test that uses the fixture once per value:
Set autouse=True to apply a fixture to every test in its scope without naming it. Use this sparingly, for cross-cutting needs such as resetting a global cache:
Hidden setup makes tests harder to read, so prefer explicit arguments when the test depends on the result.
| Aspect | unittest setUp | pytest fixture |
|---|---|---|
| Reuse | Inherited through classes | Requested by name, shared across files |
| Granularity | One setup for every test in the class | Each test requests only what it needs |
| Composition | Manual calls | Fixtures can depend on fixtures |
| Cleanup | tearDown | yield in the fixture |
Yes. Returning a factory function is a common pattern when each test needs a differently configured object, for example make_user(name="Asha").
Run pytest --fixtures to list built-in and plugin fixtures, and pytest --fixtures-per-test to see which fixtures each test uses.
No. pytest raises an error if you call a fixture function like a normal function. Request it as an argument, or move shared logic into a plain helper that both the fixture and your code call.
No. If setup is one line and used by one test, write it inside the test. Use a fixture when setup is repeated, needs cleanup or hides noise.
A fixture is a decorated function that provides setup to tests through parameters. By default each test gets a fresh instance, fixtures can build on other fixtures, and built-in ones such as tmp_path and monkeypatch cover many needs. Use params to run tests across variants and keep autouse for truly global concerns.