Property-based testing is a testing approach that focuses on verifying general properties of code, rather than specific examples.
Instead of writing individual test cases with predefined inputs and expected outputs (as in unit testing), a property-based test randomly generates input data adhering to given constraints, and checks that your code respects the required properties in all the executions driven by the randomly generated inputs. This approach helps uncover hidden bugs by exposing unexpected behaviors that unit tests might miss.
We show in this repository some examples of property-based testing in Hypothesis (Python) and QCheck (OCaml).
- Take / drop elements from a list
- Lists are functors
- All elements of a list satisfy a predicate
- Insertion sort
- Merge sort
- George Fink, Matt Bishop: Property-based testing: a new approach to testing for assurance, 1997
- D. R. MacIver: In praise of property-based testing, 2019
- J. Hughes: How to Specify It!: A Guide to Writing Properties of Pure Functions, 2020 (video)
- Choosing properties for property-based testing
- Python Hypothesis library
- Solving the Water Jug Problem from Die Hard 3 with TLA+ and Hypothesis
- Rule Based Stateful Testing
- Qcheck github
- Michael Ryan Clarkson: Randomized Testing and QCheck