liveBook · Manning

Characterization testing

As explained in Build High Value Software Systems

A characterization test records and verifies the observed behavior of existing software, including behavior that may be undocumented.

Characterization tests record what existing software actually does—including bugs and undocumented rules—so its structure can change without changing its behavior. section 7.4

To establish a useful baseline:

  • Run the code with realistic inputs and note surprising outputs, branches, and thresholds. section 7.5
  • Test typical paths, discovered edge cases, and boundary conditions. Assert what happens, not what ought to happen. section 7.5
  • After each structural change, rerun the tests; passing tests indicate that the captured behavior remains intact. section 7.4
Listing 7.10 Testing approach to capture known behavior as you see it
def test_discount_calculation_characterization():
    # Capture actual behavior, even if it seems wrong
    assert calculate_discount(100, 'SUMMER') == 15.0
    assert calculate_discount(0, 'SUMMER') == 0.0
    assert calculate_discount(-50, 'SUMMER') == 0.0  # Bug? Maybe. Current behavior.
    assert calculate_discount(100, 'INVALID') == 0.0
    assert calculate_discount(1000, 'VIP') == 250.0
    assert calculate_discount(999, 'VIP') == 99.9   # Why does threshold matter? Document it.

The discount examples capture ordinary results, invalid inputs, negative values, and a surprising threshold difference. section 7.4

How Build High Value Software Systems covers it

How deep it goes
Captures typical paths, edge cases, thresholds, and complex outputs without redefining intended behavior.
Examples
Demonstrates discount calculations with negative inputs, invalid types, and differing VIP thresholds.
What you can do after
Apply characterization tests to preserve observed behavior while restructuring legacy code.
How it connects
Links characterization testing to refactoring, code coverage, and approval testing.

Also explained in

Compare how 2 books explain characterization testing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build High Value Software Systems. The section links point to the parts of the book it draws on.