Gathering Statistics: Collecting

Learn about the collect function and how to use it in properties.

We'll cover the following

Collect

The collect(Value, PropertyResult) function allows us to gather the values of one specific metric per test and build stats out of all the runs that happened for a property. It’s special in that we need to use it to wrap the actual property result and add context to it. Let’s go through using the function and understanding how it works. The function takes two arguments, which are Value and PropertyResult.

Value represents the metric from which the statistics will be built. PropertyResult is the result of the property. The collect function takes both of these values and wraps them in a way that lets PropEr gather the metrics as well as validate the properties.

Code

A very basic property to use the collect function would look like this:

prop_collect1() ->
    ?FORALL(Bin, binary(), collect(byte_size(Bin), is_binary(Bin))).

The first argument is the metric from which we want to build statistics. In this case, the first argument is the binary’s length. The second argument is the result of the property. The collect/2 function takes both values and wraps them up in a way that lets PropEr both gather the metrics and validate the properties.

Let’s see the following property run in the terminal below.

Get hands-on with 1200+ tech skills courses.