Search⌘ K
AI Features

Dividing with let_shrink

Explore how to optimize counterexample shrinking in property-based testing by using the let_shrink macro. Understand how dividing data structures aids in isolating failure cases, improving test efficiency and debugging in PropEr with Elixir.

We'll cover the following...

The let_shrink macro

As we use PropEr, we sometimes get stuck with generators creating huge data structures that take a long time to shrink and often don’t give very interesting results back. This often happens when some very low-probability failure is triggered, meaning that the framework had to generate a lot of data to find it, and has limited chances of shrinking things in a significant manner.

Whenever that happens, the let_shrink([Pattern, ...], [Generator, ...], Expression) is what we need. Let’s take a look at how we can use the generators in practice.

let_shrink([
    a <- list(number()),
    b <- list(number()),
    c <- list(number())
]) do
    a ++ b ++ c
end

The macro looks a lot like a regular let macro, but with a few constraints. The first two arguments must always be lists, and the third argument is an operation where all list ...