Putting It All Together
Put everything from the previous lessons in the chapter together and get familiarized with writing our own properties.
We'll cover the following...
To get started with writing our own properties, it may be useful to compare them with the type of tests we’d already have in place in an existing project. We’ll start with a simple function, one that finds the largest element in a list. Coming up with properties isn’t easy, so beginning with regular tests can sometimes be a good idea since it can help us flesh out what we think the code should be doing.
Standard testing
Standard unit tests would probably look a bit like this, using the EUnit syntax:
biggest_test() ->?assert(5 =:= biggest([1, 2, 3, 4, 5])),?assert(8 =:= biggest([3, 8, 7, -1])),?assert(-5 =:= biggest([-10, -5, -901])),?assert(0 =:= biggest([0])).
We put together a list of assertions with regular unit tests and hope that it will cover all cases. Here we check for presorted lists, lists without any specific order, single-element lists, and lists containing any positive or negative numbers. It’s hard to say if any other edge cases could exist without previous experience.
Property-Based Testing
With properties, we should be able to get far better coverage of edge cases, including those we wouldn’t think of by ourselves. For a property, we need to find a rule to describe the behavior of the biggest/1
function according to these examples. The obvious rule ...