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.
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- pytest Tutorial: Effective Python Testing (Tutorial)
- Python's unittest: Writing Unit Tests for Your Code (Tutorial)
- Understanding the Python Mock Object Library (Tutorial)
- Python's assert: Debug and Test Your Code Like a Pro (Tutorial)
- Build a Hash Table in Python With TDD (Tutorial)
- Testing Your Code With pytest (Course)
- Effective Testing with Pytest (Quiz)
- Testing Your Code With Python's unittest (Course)
- Python's unittest: Writing Unit Tests for Your Code (Quiz)
- Improving Your Tests With the Python Mock Object Library (Course)
- Exploring unittest.mock in Python (Course)
- Understanding the Python Mock Object Library (Quiz)
- Using Python's assert to Debug and Test Your Code (Course)
- Python's assert: Debug and Test Your Code Like a Pro (Quiz)
- Build a Hash Table in Python With TDD (Quiz)