What is shrinking?

Shrinking is one of the most critical components of property-based testing. It is the system by which a property-based testing framework can be told how to simplify failure cases enough to let us figure out precisely what the minimum reproducible case is. While finding complex obtuse cases is worthwhile, being able to reduce failing inputs to a simple counterexample truly is the killer feature.

Shrinking of Dataset

The positive side to shrinking

When a large dataset faces a failure, it can be challenging to find the initial failure case due to irrelevant data. In this case, the framework can shrink data in the following ways:

  • A number tends to shrink from floating-point values toward integers.
  • Integers tend to shrink toward the number 0.
  • Binaries tend to shrink from things full of bytes toward the empty binary.
  • Lists tend to shrink toward the empty list.
  • Elements([A,B,C]) will shrink toward the value A.

The negative side of shrinking

On the other side, shrinking is not a good zero-point for some data types. These are shown below:

  • A vector of length 15 will always have length 15.
  • A tuple has the same issue as vector.
  • Larger recursive data structures that have been defined by the user may not have obvious ways to shrink.

There are two ways to handle things that can be used to impact shrinking:

  • ?SHRINK
  • ?LETSHRINK

?SHRINK

Conceptually, ?SHRINK is the simplest of the two macros that can be used to impact shrinking. It is best used to pick a custom zero-point toward which PropEr will try to shrink data. The DefaultGenerator will be used for all passing tests.

In Erlang, macro takes the form ?SHRINK(DefaultGenerator, [AlternativeGenerators]).

In Elixir, macro takes the form shrink(default_generator, [alternative_generators]).

?LETSHRINK

In PropEr, we sometimes get stuck with generators that are creating huge data structures, take a long time to shrink, and often don’t give very interesting results back. Whenever this happens, we need the the ?LETSHRINK([Pattern, ...], [Generator, ...], Expression).

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved