State-based testing
State-based testing is a testing style that verifies observable state produced or changed by the code under test.
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
- output-based testingcontrasts with · Output-based testing evaluates software by comparing explicitly produced outputs with expected outputs rather than verifying internal interactions.
- testkind of · A test is a procedure or check that evaluates behavior using specified inputs or evidence against expected results or criteria.
- classical schoolcontrasts with · A unit-testing approach, also called the Detroit school or classical style, that isolates tests from one another while allowing the unit under test to use real collaborator objects.
- value objectuses · A value object is an object whose meaning and equality derive from its attribute values rather than an independent identity, typically with immutable state.
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.

