A property-based testing library separates behavioral specifications from test-case construction. You state properties and high-level input constraints; the library generates satisfying cases and checks whether the properties hold. section 8.0
A typical workflow combines four parts:
- Define a generator such as Gen[List[Int]] that produces constrained values, such as lists containing random integers from 0 through 100. section 8.1
- Give Prop.forAll a generator and a predicate of type A => Boolean; the resulting property requires every generated value to satisfy the predicate. section 8.1
- Compose properties with && so a specification succeeds only when none of its generated cases falsifies either property. section 8.1
- Run check to generate test cases, report success, or print a counterexample that supports debugging. section 8.1
Testing list reversal
A specification can require that reversing a list twice returns the original list and that the first optional element matches the reversed list’s last optional element. A deliberately false property, such as requiring every list to equal its reverse, is falsified with a concrete counterexample such as List(0, 1).
