Introduction to State Machine Properties

Get introduced to state machine properties and why there is a need to use them.

Finite state machines

Finite state machines (FSM) are, informally speaking, an abstraction describing specific stateful programs. The program can be in only one of the multiple known states at any given time and transitions from one state to another based on specific inputs or events. For instance, a traffic light can only be red, yellow, or green at any one time and might change from one color to another based on an automated timer or a trigger like a person hitting the cross-street button. Within each state, the program may behave differently.

Why use state machine properties

Let’s imagine a program that takes special measures depending on whether network connectivity is good, bad, or entirely unavailable. These measures could include forbidding certain actions or enabling new features. Testing this program with stateful properties while keeping them deterministic would create an explosion of possible cases. If there are six possible events and three possible states, we quickly get at least eighteen possible combinations of calls versus states. We would have to do quite a bit of filtering in preconditions to hope to cover all kinds of transitions.

To help with this, PropEr provides finite state machine properties, which allow us to specifically model state machines. State names and transitions are made into first-class citizens, which allows us to adequately explore their expected transitions in a less cumbersome manner than would be required with regular stateful properties or example tests.

Finite-state properties are specifically helpful when users interact with the system while perceiving multiple states rather an internal state machine. To put it another way, state machine properties are for when the model itself is a state machine, not the system. If there are no specific, discernible states for the user, and therefore no specific, discernible states for the model, regular stateful properties are more appropriate.

Get hands-on with 1400+ tech skills courses.