liveBook · Manning

State-based testing

State-based testing is a testing style that verifies observable state produced or changed by the code under test.

How 2 Manning books explain it

Unit Testing Principles, Practices, and Patterns

A unit-testing style that verifies the state of the system under test, a collaborator, or an out-of-process dependency after an operation.

How it is used
Verifies post-operation state when behavior is not captured solely by a return value.
How deep it goes
Defines the style, compares trade-offs, demonstrates assertions, and stops short of requiring total conversion to output-based testing.
Examples
Uses an Order collection, Article comments, and a filesystem-backed audit system to demonstrate state assertions and refactoring.
What you can do after
Apply it by asserting observable system, collaborator, or dependency state while managing brittleness and maintenance cost.

Read how Unit Testing Principles, Practices, and Patterns explains state-based testing →

The Art of Unit Testing 3e

A state-based test verifies behavior by examining the observable state produced or changed by the code under test.

How it is used
Verifies indirect outputs by checking changed state through another call or sibling property rather than the entry point’s return value.
How deep it goes
Defines indirect outputs, explains follow-up calls and sibling observables, and contrasts them with internal-call verification.
Examples
Uses follow-up calls, sibling functions or properties, and PasswordVerifier4’s protected findFailedRules() example.
What you can do after
Apply it by asserting observable state after invoking the entry point, while avoiding internal-call verification when possible.

Read how The Art of Unit Testing 3e explains state-based testing →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.