liveBook · Manning

Test name

As explained in The Art of Unit Testing 3e

The textual name assigned to a test to communicate the tested unit, scenario, and expected behavior or exit point.

A test name is the reader’s fast explanation of what a test checks. A useful name states three things: the unit under test, the scenario or inputs, and the expected behavior or exit point. This USE pattern—unit, scenario, expectation—makes a failed test informative even when the runner shows only its name. section 2.5.3 section 9.1

Include information that answers the reader’s immediate questions:

  • Name the entry point or feature under test, such as verifyPassword. section 9.1
  • State the scenario, including the relevant input or condition, such as a failing rule. section 2.5.3
  • State the expected observable behavior, such as returning an error based on rule.reason. section 9.1

Name one concern clearly

`verifyPassword, given a failing rule, returns errors` tells a reader what runs, under which condition, and what should happen. The same information can be split across nested `describe` and `it` labels, as long as all three parts remain nearby. Names that omit the unit, scenario, or expectation force readers to inspect test code. section 2.5.3 section 9.1

A vague name often signals that a test checks too many concerns. Separate tests for separate exit points keep names focused and preserve diagnostic information: when one assertion fails, later assertions may not run. Multiple assertions can still fit one test when they verify the same exit point. section 7.4.4 section 5.6.4

Listing 2.5 Naming a test with USE
test('verifyPassword, given a failing rule, returns errors', () => {
  const fakeRule = input => ({ passed: false, reason: 'fake reason' });
 
  const errors = verifyPassword('any value', [fakeRule]);
  expect(errors[0]).toContain('fake reason');
});

This small test shows a USE-style name alongside the scenario setup and assertion it summarizes. section 2.5.3

How The Art of Unit Testing 3e covers it

How deep it goes
Defines a three-part naming pattern and compares full names, nested labels, and names missing required information.
Examples
Demonstrates verifyPassword with a failing rule and trigger tests split between a callback and return value.
What you can do after
Evaluate/understand whether a test name identifies its unit, scenario, and expected behavior without requiring code inspection.
How it connects
Connects test names to unit tests, exit points, assertions, test readability, mocks, and test isolation.

Also explained in

Compare how 2 books explain test name →

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.