liveBook · Manning

Behavior

As explained in The Art of Unit Testing 3e

Behavior of software that can be detected through outputs, state changes, or other effects visible to an external observer.

Behavior is what software reveals to an external observer through outputs, state changes, or other visible effects. Tests should verify this user-relevant behavior rather than private implementation details, which makes them less fragile. section 8.4

Use behavior as the boundary for deciding what a test should describe and assert:

  • Name the unit of work or feature, describe the scenario, and state the expected behavior visible at the exit point. section 9.1 section 9.5
  • Avoid asserting private state, calls on stubs, collection order, or exact strings when those details are not required by the behavior. section 8.4
  • Treat a need to test a private method as a possible sign that a missing abstraction should become public or move into another class. section 8.4

A behavior-focused test name

test('verifyPassword, with a failing rule, returns error based on rule.reason', () => { ... })

Listing 9.1 Same information, different variations
test('verifyPassword, with a failing rule, returns error based on rule.reason', () => { ... }

describe('verifyPassword', () => {
 describe('with a failing rule', () => {
   it('returns error based on the rule.reason', () => { ... }

verifyPassword_withFailingRule_returnsErrorBasedonRuleReason()

Compare a single descriptive test name, nested describe structures, and a method-style name expressing the same unit, scenario, and expected behavior. section 9.1

How The Art of Unit Testing 3e covers it

How deep it goes
Defines observable behavior and applies it to assertions, test names, overspecification, and private-method decisions.
Examples
Uses verifyPassword with a failing rule to show a test name containing the unit, scenario, and expected result.
What it assumes
Assumes units of work, test scenarios, assertions, and private methods while leaving language and framework unspecified.
What you can do after
Evaluate tests for user-relevant behavior and write names and assertions that avoid unnecessary implementation coupling.
How it connects
Connects observable behavior with unit testing, test maintainability, private methods, abstractions, and test naming.

Also explained in

Compare how 4 books explain behavior →

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.