liveBook · Manning

Test isolation

As explained in Unit Testing Principles, Practices, and Patterns

The property of executing a test independently so that other tests or external influences cannot affect its outcome.

Test isolation is the quality of executing a test independently, so other tests or external influences cannot affect its outcome. Unit tests verify a small piece of code quickly and in an isolated manner; the disputed question is whether isolation applies primarily to the system under test or to the tests themselves. section 2.1 section 5.0

Two influential interpretations make different trade-offs:

  • The London style isolates the system under test from its collaborators by replacing dependencies with test doubles, concentrating each test on one class. section 2.1
  • The classical style isolates tests from one another so they can run in parallel, retaining private dependencies and replacing only dependencies shared between tests. section 5.0
  • Mocks can support isolation but often create fragile tests that resist refactoring; use them where their benefits outweigh that maintenance cost. section 5.0

Inventory purchase

A classical test constructs a store, adds ten shampoos, and verifies that purchasing five succeeds while inventory falls to five. A London-style test replaces the store with a mock, stubs the inventory check, and verifies that inventory removal occurs once.

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Contrasts two isolation boundaries, demonstrates each with inventory tests, and links mocks to fragility and refactoring resistance.
Examples
Uses inventory-purchase tests to contrast concrete state verification with mocked interaction verification.
What you can do after
Evaluate isolation strategies and apply test doubles while accounting for parallel execution, shared dependencies, and refactoring resistance.
How it connects
Connects unit testing with test doubles, mocks, test fragility, and the classical and London schools.

Also explained in

Compare how 3 books explain test isolation →

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.