Skip to content

Fixture

A test fixture is the fixed, known state a test relies on: the data, files, database rows and configuration prepared before the test runs and torn down after, so the result is reproducible.

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

  1. Phase 1 — Fixture setup: create the objects, rows or files the test needs.

  2. Phase 2 — Exercise SUT: call the one method or function under test.

  3. Phase 3 — Result verification: assert on the returned value or the state left behind.

  4. 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

  1. Create only what the test reads

    If the test asserts on two rows, insert two rows. A 500-row dump hides which row mattered.

  2. Use a factory, not a dump

    A factory makes the data visible in the test. A dump hides it in a file.

  3. Tear down in reverse order

    If you opened a transaction, then a file, then a socket, close the socket, the file, then the transaction.

  4. Name the fixture after the scenario

    a_user_with_expired_card, not setup2. 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

Etymology

Origin: borrowed

  • fixture (noun): from Latin fixus, fixed; something fastened in place

Semantic network

Fixturesoftware-txunitStubMockTest doublfour-phase

● broader ● narrower ● related

Broader

  • software-testing
  • xunit

Related

Sources