Putting It All Together

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 all easy, so beginning with regular tests can sometimes be a good idea since it can at least help us flesh out what we think the code should be doing.

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])).

With regular unit tests, we put ...

Get Property-Based Testing with PropEr, Erlang, and Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.