Property-based testing
In short: In property-based testing you state properties that must always hold, and the test tool tries them with many random inputs.
In more detail: Instead of “sort([3,1,2]) gives [1,2,3]” you check “the result is sorted and has the same elements as the input”. The tool generates hundreds of inputs and looks for counterexamples.
In Depth
- Shrinking: When it finds a failure, it reduces the input to the smallest example that still fails.
- Good properties: Round trips (
decode(encode(x)) == x), idempotence, invariants (length stays equal), comparison with a simple reference. - Tools: Hypothesis (Python), fast-check (JavaScript), QuickCheck (Haskell), jqwik (Java).
- Advantage: Finds edge cases you did not think of (empty inputs, huge numbers, special characters).
- Complement: Does not replace normal example tests but supplements them.
See also: Mutation testing