Stateful Property-Based Testing
Understand how to apply property-based testing to stateful systems in Elixir. Learn to model system state, generate random commands, and verify that real system behavior aligns with the model. Discover how this approach helps catch complex bugs through shrinking failing test cases.
Until now, we’ve mostly looked at property-based testing in the context of testing pure, stateless functions that take an input and return an output. However, property-based testing is also useful for testing stateful systems.
What is the stateful system?
A stateful system is a system that, well, carries the state. For example, a database is a stateful system.
In our examples so far, we only used property-based testing to generate some data and then feed it to a piece of code and assert the result of that. With stateful systems, things change: we now have to deal with setting a state and only executing some operations when the system is in a given state. Let’s see how we can use property-based testing for something like that.
Modeling the Stateful System
We know how to generate random data through our property-based testing framework. We can take advantage of this knowledge to generate random commands that we can issue on our stateful system.
For example, if our stateful system is a database, we can generate random commands to issue against this system. However, if the commands are random, how do we assert ...