Search⌘ K
AI Features

Writing the First Property

Explore writing your first property in PropEr to test Erlang code. Understand how to build generators for items, prices, and expected values. Learn to create straightforward properties, implement the tested functions, and validate the test coverage effectively. This lesson sets the foundation for property-driven development with practical examples.

Getting started

The first property doesn’t have to be advanced. In fact, it’s better if it’s simple. Start with something trivial-looking that represents how we want to use the program. Then, our job as developers is to make sure we can write code that matches or changes our expectations. Of our two properties, the simplest one concerns counting sums without caring about specials.

We’ll want to avoid a property definition, such as sum(ItemList, PriceList) =:= checkout:total(ItemList, PriceList, []), since that would risk making the test similar to the implementation. A good approach to try here is generalizing regular example-based tests. Let’s imagine a few cases:

20 = checkout:total(["A","B","A"], [{"A",5},{"B",10}], []),
20 = checkout:total(["A","B","A"], [{"A",5},{"B",10},{"C",100}], []), 
115 = checkout:total(["F","B","C"], [{"F",5},{"B",10},{"C",100}], []),

That’s actually tricky to generalize. It’s possible that, to come up with examples, we just make a list of items, assign them prices, pick items from the list, and then sum them up ourselves. Even if it’s not really straightforward, we can build on that. The ...