liveBook · Manning

Constrained test order

As explained in The Art of Unit Testing 3e

Constrained test order is an antipattern in which a test depends on another test having executed or not executed first.

Constrained test order occurs when one test depends on another test having run—or not run—because both rely on shared state. A test runner may change the order, exposing failures that are difficult to diagnose. section 8.1.3

A shared cache creates the dependency

One test adds user "a" to a shared UserCache; a later test expects login for that user to succeed. The first test fails if the cache already contains the user, while the third fails if the second has not run. Running only the third test also reveals the hidden setup dependency.

Remove order dependence by making each test establish its own state:

  • Treat tests as independently runnable rather than relying on runner order. section 8.1.3

How The Art of Unit Testing 3e covers it

How deep it goes
Explains the dependency, demonstrates its failure modes, and shows refactoring with reset and local setup.
Examples
Uses a shared UserCache, SpecialApp login tests, single-test execution, and beforeEach-based resetting.
What you can do after
Diagnose and refactor order-dependent tests by resetting shared state and creating setup within each test.
How it connects
Connects test isolation with shared state, singleton objects, and test-runner execution order.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Unit Testing 3e. The section links point to the parts of the book it draws on.