Invariants
Explore the concept of invariants within property-based testing and learn how to identify and use them to ensure reliable software behavior. Understand how combining multiple simple invariants forms a strong test suite, illustrated by examples such as sorting functions. This lesson helps you apply invariant-based properties effectively using PropEr in Erlang.
We'll cover the following...
What are invariants?
Some programs and functions are complex to describe and reason about. They might need many small parts to work correctly to be functional, or we may not be able to assert their quality because it’s difficult to define. For example, a judgment about whether a meal is good is subjective, although it could include criteria. The criteria could include whether the ingredients were cooked adequately, whether the food is served at the correct temperature, whether the meal is too salty, sweet, or bitter, etc. These factors play a part in the decision of “whether the food is good or not.”
Similarly, in a software system, we can identify similar conditions that should always remain true. These are called invariants.
Advantages of invariants
Testing with invariants is a great way to get around the fact that things may just be ambiguous otherwise. If an invariant were to be false at any time, we would know something isn’t functioning properly. ...