Recording Random Choices in Tests

A randomized test should preserve a failing example or enough information to reproduce it under the stated environment.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Save the concrete input
  2. Reduce before fixing
  3. Save the input actually responsible for a failure
  4. Keep independent runs useful after debugging

Save the concrete input

If a generated sequence of length nineteen breaks the program, keep the sequence itself and its expected behavior. A seed can replay a particular generator under known conditions, but the input remains useful even if the generator changes. Reproducing program runs explains other dependencies to record.

Reduce before fixing

Minimize the sequence while preserving failure. A three-item counterexample may reveal an equal-key tie that was hidden in the original case. Turn it into a direct regression test.[1]

Save the input actually responsible for a failure

An ordering property fails on a generated case containing three equal priorities. Save the seed and simplify the failing list if your testing tool can do so. An explicit failing value remains replayable even if generation strategy or library changes. See replay seeds.

Environment: named generator and configuration
Saved case: tied keys with three distinct IDs
Expected: preserve their original order
Turn the concrete failure into a direct regression fixture

Keep independent runs useful after debugging

Replaying one seed helps diagnose one behavior. Testing newly generated values can reveal other issues. Make a failing run display its case and distinguish a genuine property violation from an environment-dependent failure.

References

  1. Hypothesis Contributors, Introduction to Hypothesis ↩