Writing Properties

Learn to write property-based tests.

As we saw in our initial examples, part of property-based testing is generating data and the other part is coming up with properties. In this and the next lessons, we’re going to focus on the latter.

Shape of inputs

The first thing to do when coming up with properties is to figure out the shape of valid inputs that our code works with.

“Shape“ is not a technical term or a definition: we’re using it to describe the space of possible values that the input can be, such as all strings or all lists of integers.

Figuring out the shape of valid inputs is where generators come into play in the context of property-based testing. If our code works with lists of integers, and therefore the shape of valid inputs is “lists of integers,” we can use the StreamData.list_of(StreamData.integer()) generator to produce a sample of the possible values of that shape. We create and combine generators that output valid data in the shape accepted by our code.

After figuring out the valid inputs that our code accepts, we need to come up with properties that our code holds regardless of the input, provided that the input is valid.

Once we have generators to produce inputs of our desired shape and properties that hold for all of those inputs, it’s the job of the property-based testing framework to provide the infrastructure for generating data and verifying the properties. The stream_data comes bundled with tools and a DSL that lets us do exactly that. We know about stream_data generators by now, so the missing piece is using those generators to produce inputs and test properties against those inputs. Let’s start with rewriting our properties for the Enum.sort/1 function using the tools provided by stream_data in order to have a look at what stream_data in action looks like. Then we’ll examine the snippet in more detail.

Our first property-based test

Take a look at the first property based test in property_based_testing/sorting/test/first_property_sort_test.exs

Get hands-on with 1200+ tech skills courses.