How-to guide

How to write and replay a property-based test

How do you turn a general rule into a property-based test?

State a rule that should hold across a family of inputs, generate examples from that family and check the rule for each. When a case fails, shrink it and save enough information to reproduce the failure on the same version.

Design the property, generator and replay together

A good generator reaches the parts of the input space where you expect the rule to be challenged.

  1. Write the rule in plain language and give a small example that would violate it. Agree any preconditions.
  2. Generate representative values, including empty, repeated and boundary values where valid. Check both the quantity and variety of cases that actually ran.
  3. For a failure, preserve the smallest reproducible input, seed, runner version and replay information. Keep the small case as a regression alongside the broader property.

Worked example

A packing list must preserve every item

Generate lists of stock codes that can contain duplicates. After splitting a list into parcels, flatten the result and compare the count of each code with its original count. A bug that silently drops repeated items might shrink to a list containing just two identical codes. That pair makes a precise regression example.

Try it on your pull request

Choose a change in your repository and tell Cue April what should happen. Review the proposed checks before they run.

Try Cue April on your repository