liveBook · Manning

Test fixture

As explained in Unit Testing Principles, Practices, and Patterns

A test fixture is an object or external resource placed in a known state for a test to exercise.

A test fixture is an object, database record, or file placed in a known state before a test runs, so the test produces a consistent result. section 3.3

Reuse fixture setup without hiding each test’s assumptions:

  • Prefer private factory methods for shared arrange logic; they reduce duplication while keeping setup visible and tests decoupled. section 3.7
  • Avoid constructors or setup methods for ordinary shared arrangements because changes can couple every test and obscure its initial state. section 3.3

Visible fixture factories

A purchase test can create `Store store = CreateStoreWithInventory(Product.Shampoo, 10);` and `Customer sut = CreateCustomer();` inside its arrange section, preserving the scenario’s important assumptions.

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines fixtures, contrasts constructor setup with factory methods, and demonstrates both unit-test and integration-test arrangements.
Examples
Uses inventory stores, customers, products, database fixtures, constructors, factory methods, and integration-test base classes.
What it assumes
Assumes familiarity with test arrange sections and distinguishes ordinary fixtures from NUnit’s TestFixture class attribute.
What you can do after
Apply fixture reuse through factory methods while keeping each test’s relevant setup visible.
How it connects
Connects test fixtures with arrange sections, unit tests, factory methods, test coupling, readability, and integration tests.

Also explained in

Compare how 10 books explain test fixture →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.