liveBook · Manning

Unit of behavior

As explained in Unit Testing Principles, Practices, and Patterns

A unit of behavior is a cohesive, domain-meaningful piece of externally observable functionality verified by a test.

A unit of behavior is a cohesive, domain-meaningful piece of functionality verified by a test. It may span multiple classes, one class, or a small method; implementation size is irrelevant. section 2.3

A useful test tells a coherent story about a problem the software solves and describes behavior recognizable to a non-programmer. Testing smaller code fragments can obscure what the test actually verifies. section 2.3

Distinguish a unit of behavior from a unit of code:

  • Prefer observable outcomes over individual classes or implementation steps. section 2.3
  • Treat the number of collaborating classes as irrelevant when one cohesive behavior is being verified. section 2.3
  • Recognize tests focused on implementation details as vulnerable to unclear stories and over-specification. section 2.5

Calling a dog

“When I call my dog, he comes right to me” describes one meaningful behavior. Listing each leg movement, head turn, and tail movement instead targets implementation-like details and produces a less coherent test story.

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines the boundary, contrasts behavior with code units, and illustrates the distinction through a concrete test-story comparison.
Examples
Uses a dog coming when called versus separately listing its leg, head, and tail movements.
What you can do after
Evaluate whether a test verifies one cohesive, domain-meaningful behavior instead of coupling itself to implementation details.
How it connects
Connects unit testing with observable behavior, implementation details, test doubles, and the classical and London schools.

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.