Definition
What it is
A fixture is not a test double. It is the world the test runs in. It includes the objects under test, their collaborators, any files the test reads, the rows it queries and the environment variables it depends on. A fixture that is not prepared is a test that depends on whatever the previous test left behind.
The term comes from the xUnit family of frameworks, which formalized the pattern around four phases: set up the fixture, exercise the system under test, verify the result, tear down the fixture.
The four-phase test
Phase 1 — Fixture setup: create the objects, rows or files the test needs.
Phase 2 — Exercise SUT: call the one method or function under test.
Phase 3 — Result verification: assert on the returned value or the state left behind.
Phase 4 — Fixture teardown: release resources, drop the database, delete the files.
Fixture vs stub vs mock
Aspect | Fixture | Stub | Mock |
|---|---|---|---|
What it prepares | The environment and data | A canned answer | An expectation |
When it acts | Before and after the test | During the call | During and after |
Fails the test? | Only if setup throws | No | Yes, on unmet expectation |
Typical example | A database seeded with three users | A clock that always returns noon | A payment gateway that must be charged once |
Fresh vs shared fixtures
How to design one
A fixture that explains the test
Create only what the test reads
If the test asserts on two rows, insert two rows. A 500-row dump hides which row mattered.
Use a factory, not a dump
A factory makes the data visible in the test. A dump hides it in a file.
Tear down in reverse order
If you opened a transaction, then a file, then a socket, close the socket, the file, then the transaction.
Name the fixture after the scenario
a_user_with_expired_card, notsetup2. The name is the documentation.
Origin
The word entered English in the 16th century meaning "something fixed or attached", from Latin fixus, past participle of figere, "to fasten". By the 19th century it meant a piece of equipment permanently attached to a property ("light fixtures", "bathroom fixtures"). The xUnit community borrowed it in the late 1990s to name the fixed baseline a test needs: the state that must be in place before the test can be meaningful.
Evidence
FAQ
- Is a fixture a test double?
No. A test double replaces a collaborator. A fixture prepares the world, and it can contain doubles.
- Fresh or shared?
Fresh by default. Share only when the setup is genuinely expensive and the fixture is immutable.
- Fixture vs factory?
A factory builds objects inside the test, visible in code. A fixture file is loaded from outside, visible only if you open it.
- Do fixtures belong in the repository?
Small, deterministic fixtures yes. Large database dumps no; generate them from a seed script.
Connections
- stub
- mock
- test-double
- four-phase-test
- xunit
Meszaros, G. (2007). xUnit Test Patterns. Addison-Wesley. https://martinfowler.com/books/meszaros.html
Wikipedia (2024). xUnit. https://en.wikipedia.org/wiki/XUnit
pytest documentation (2024). About fixtures. https://docs.pytest.org/en/stable/explanation/fixtures.html
Etymology
Origin: borrowed
fixture(noun): from Latin fixus, fixed; something fastened in place
Semantic network
● broader ● narrower ● related
Sources
Meszaros, G. (2007). xUnit Test Patterns. https://martinfowler.com/books/meszaros.html
Wikipedia (2024). xUnit. https://en.wikipedia.org/wiki/XUnit
pytest documentation (2024). About fixtures. https://docs.pytest.org/en/stable/explanation/fixtures.html