Introduction to Stateful Properties

Get started with stateful properties and their basic underlying concepts.

Introduction to stateful properties

If you’ve seen a situation where a tricky bug was found with a relatively tiny test, chances are that was stateful properties at work. Stateful property tests are particularly useful when what the user perceives is simple but the code implemented is complex. A large number of integration and system tests fit this description, and stateful property-based testing will become one of the most interesting tools to have since it can exercise major parts of our systems with little code.

Stateful property tests are an informal variation of model checking. The core concept is that we must define a mostly predictable model of the system, and then use PropEr to generate a series of commands that represent operations that can be applied to the system. PropEr then runs these operations on both the model and the actual system and compares the two. If they agree, the test passes; if they disagree, it fails.

Let’s get started with the theory of it all.

Laying out stateful properties

Stateful properties all have a few parts in common, with a bit more scaffolding than we had in stateless ones. It’s important to keep these parts in mind because they’ll be interacting with each other for all test executions. These are the three major components:

  • A model, which represents what the system should do at a high level.
  • A generator for commands, which represent the execution flow of the program.
  • An actual system, which is validated against our model.

Get hands-on with 1200+ tech skills courses.