Free — no signup required

The Magic of "Shrinking

1 min read

Finding the Needle in the Haystack

Imagine a PBT engine generates a random list of 5,000 integers that causes your sorting function to crash. Debugging a 5,000-item list is a nightmare. You don't know which specific combination of numbers triggered the failure. Without help, PBT would be nearly unusable in practice.

Shrinking to the Minimal Failing Example

Modern PBT libraries solve this with a feature called Shrinking. When the engine finds a failing input, it does not immediately report it. Instead, it enters a reduction loop:

  1. Take the failing input (5,000 items).
  2. Try removing elements, simplifying values (replacing large numbers with small ones, replacing complex strings with shorter ones).
  3. Re-run the test after each simplification.
  4. If the test still fails, keep the simplification and try to shrink further.
  5. If the test passes, discard the simplification and try a different reduction.
  6. Stop when no further simplification causes a failure.

The result: instead of reporting a 5,000-item list, Hypothesis might report: "Your function fails when the input is [0, 2147483647]" — the minimum reproducible case.

This transforms a confusing random failure into a precise, debuggable bug report. It is the feature that makes PBT practical rather than just theoretically interesting.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free