Skip to content

test fixture

A test fixture is the fixed, known state that a software test relies on to run. It includes the input data, the environment, and any preconfigured objects or external systems that the code under test interacts with.

Fixtures keep tests repeatable and isolated by guaranteeing that each run starts from the same baseline. The xUnit family of frameworks formalizes this around the four-phase test pattern: a test sets up the fixture, exercises the system under test, verifies the result, and tears the fixture down. Setup may be inline in the test, in a delegated helper, or in a dedicated setup method shared across multiple tests.

Common forms of test fixtures include:

  • Factory objects or seeded data structures in memory
  • Database rows loaded from YAML or SQL files
  • Files placed at known paths or temporary directories
  • Stub or mock collaborators in place of live external services
  • Hardware or device states in embedded and integration testing

A fixture may be fresh and rebuilt for each test, or shared across a suite for performance. Shared fixtures save setup time, but they can leak state between tests and weaken isolation.

Tutorial

How to Provide Test Fixtures for Django Models in Pytest

In this step-by-step tutorial, you'll learn how to use fixtures to simplify your testing in pytest. If you use Django, pytest fixtures can help you write tests that are painless to update and maintain even as you make changes to your models.

intermediate django testing

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated June 10, 2026 • Reviewed by Leodanis Pozo Ramos