Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Are pytest Fixtures and How Do You Use Them?

Software TestingPython
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 30, 2026
5 min read

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.

Table of Contents

  • What a fixture is and why you use it
  • Defining and requesting a fixture
  • Fixtures that use other fixtures
  • Built-in fixtures you can use immediately
  • Fixtures with parameters and automatic use
    • Parametrized fixtures
    • Autouse fixtures
  • Fixtures compared with setup and teardown methods
  • Frequently Asked Questions
    • Can a fixture return a function or a class?
    • How do I see which fixtures are available?
    • Can I call a fixture function directly?
    • Should every test use fixtures?
  • Summary
  • Related Articles

What a fixture is and why you use it

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:

python

The setup is duplicated, and it grows with each new test.

Defining and requesting a fixture

Here is the same code with a fixture:

python

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.

Fixtures that use other fixtures

A fixture can request another fixture in the same way a test does, which lets you build setup in layers:

python

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.

Built-in fixtures you can use immediately

pytest ships with fixtures for common needs. These are the ones you will use most:

FixtureWhat it gives you
tmp_pathA unique temporary directory as a pathlib.Path
monkeypatchSafely set or remove attributes, environment variables and dictionary items
capsysCaptures text written to stdout and stderr
caplogCaptures log records
requestInformation about the requesting test, used inside fixtures
python

Changes made by monkeypatch are undone automatically after the test, and tmp_path directories are cleaned up by pytest according to its retention policy.

Fixtures with parameters and automatic use

Two options change how fixtures are applied.

Parametrized fixtures

Pass params to run every test that uses the fixture once per value:

python

Autouse fixtures

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:

python

Hidden setup makes tests harder to read, so prefer explicit arguments when the test depends on the result.

Fixtures compared with setup and teardown methods

Aspectunittest setUppytest fixture
ReuseInherited through classesRequested by name, shared across files
GranularityOne setup for every test in the classEach test requests only what it needs
CompositionManual callsFixtures can depend on fixtures
CleanuptearDownyield in the fixture

Frequently Asked Questions

Can a fixture return a function or a class?

Yes. Returning a factory function is a common pattern when each test needs a differently configured object, for example make_user(name="Asha").

How do I see which fixtures are available?

Run pytest --fixtures to list built-in and plugin fixtures, and pytest --fixtures-per-test to see which fixtures each test uses.

Can I call a fixture function directly?

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.

Should every test use fixtures?

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.

Summary

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.

Related Articles

  • How Does pytest Discover Tests and Report Assertions?
  • What Are pytest Fixture Scopes and Teardown with yield?
  • Learn Software Testing: Complete Roadmap from Beginner to Advanced
Share

Similar posts

  • What Are pytest Fixture Scopes and Teardown with yield?
  • How Does pytest Discover Tests and Report Assertions?
  • How Do You Install pytest and Write Your First Test?
  • How Do You Write Unit Tests for Legacy Code?
  • What Is Code Coverage and What Percentage Is Good Enough?
  • Stub, Mock, Fake and Spy: What Are Test Doubles?
  • What Is Behavior-Driven Development (BDD) with Given-When-Then?
  • What Is Test-Driven Development (TDD) Red-Green-Refactor?