PropEr state machine testing by Max Nordlund

December 8, 2021

Max Nordlund discusses state machine testing using the PropEr library. State machines are systems with a finite set of states and transitions between them, useful for describing user journeys or account management. PropEr allows modeling state machines and testing real implementations with a harness that stress tests and finds invalid transitions. Max emphasizes the importance of modeling state transitions, including grace periods, and using Erlang and the PropEr testing framework for implementation. He also discusses challenges encountered while testing complex codebases and the benefits of explicit coding and using Dialyzer for type checking. The video covers testing properties in PropEr, generating test data, and understanding symbolic calls to determine the next state and understand the previous state of a testing module. Max also shares his experience teaching product owners about state machines and the importance of drawing clear lines and specifying business events. The state machine testing engine used in the video continuously runs events until it reaches a steady state and has logic to detect and prevent infinite recursion.

PropEr state machine testing: A comprehensive overview

Max Nordlund captivated the audience with his talk on state machine testing using the PropEr library. As a backend developer at Kivra, Max shared his wealth of experience, particularly in utilizing state machines extensively within his company.


Introduction to state machines

Max commenced the discussion by elucidating the concept of state machines, emphasizing their utility as a design tool. With a finite set of states and transitions between them, state machines offer precision and agreement between product owners and developers regarding system functionality. Nordlund illustrated this with an example of a user lifecycle on a website, setting the stage for an engaging demo promised to be both entertaining and enlightening.


Modeling state machines with PropEr

Delving deeper, Max elaborated on the significance of modeling state transitions, spotlighting the notion of a grace period in system exits. He introduced the PropEr library, renowned for its state machine testing capabilities, which enables users to stress-test real implementations and identify invalid transitions. Max underscored PropEr’s unique feature of shrinking, enhancing testing efficiency by reducing counterexample sets.


Implementation in Erlang with PropEr

Transitioning to practical implementation, Max delved into building a state machine using Erlang and the PropEr testing framework. He elucidated the module’s type definition, the state representation using binaries, and the use of Erlang’s Dialyzer type system for enforcing state conditions at the type level. Despite its intricacies, Max emphasized the effectiveness of this approach for ensuring code correctness.


Challenges and testing strategies

Max candidly discussed challenges encountered while testing complex codebases using Dialyzer and PropEr. He showcased PropEr’s property-based testing capabilities, explaining how it generates and runs test sequences while efficiently handling failures. Max outlined strategies for testing properties, including keeping bookkeeping outside the state machine and utilizing PropEr’s `forAll` macro for property testing.


Debugging and ensuring correctness

Max navigated through debugging techniques, illustrating the process of identifying and resolving failures in state machine testing. He emphasized the importance of clear state definitions and business event specifications, simplifying the concept for both developers and product owners. Additionally, Max showcased techniques for handling exceptions and shrinking tests to pinpoint failures accurately.


Empowering product development

Max concluded with insights into educating product owners about state machines, advocating for collaborative state and event definition sessions to enhance clarity and understanding. He underscored the significance of drawing clear lines and specifying business events to facilitate seamless collaboration between development teams and stakeholders.


Looking ahead

Max provided a glimpse into the future of state machine testing, discussing ongoing efforts to refine testing engines and enhance automation. While acknowledging the complexity of implementation, he stressed the importance of maintaining purity in the core codebase for seamless state machine execution.

Max Nordlund’s exploration of PropEr state machine testing offers a comprehensive understanding of its principles and practical applications. His insights not only empower developers to enhance code correctness and reliability but also foster collaboration and clarity within product development teams.


Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.