What is property-based testing, and when is it more useful than example-based testing?
Answer
Property-based testing generates many inputs from a defined domain and checks invariants that should always hold — for sorting, that means the result contains the same multiset of elements as the input, is ordered according to the comparator, and sorting an already-sorted result is idempotent, not that sorting "preserves order," since reordering is exactly what a correct sort is supposed to do. It is valuable for parsers, transformations, protocols and rule-heavy code with large input spaces. Keep a few readable examples, constrain generators to meaningful data, and make failures reproducible with saved seeds and minimized counterexamples.
Strong answer includes
- defines properties and generated input domains
- sorting properties are multiset preservation, comparator-ordering and idempotence — not preserving input order
- mentions shrinking or minimized reproducible failures
Practical example
Testing a JSON-serialization function with Hypothesis, a property like deserialize(serialize(x)) == x catches a bug where large integers silently lose precision, something an example-based suite with a handful of hand-picked numbers never happened to hit.