Property-Based Testing
Instead of listing examples, state a property that should hold for every input and let the machine generate inputs trying to break it. Good properties are relations that must survive, such as encoding then decoding returning the original.
What a learner can do afterwards
- Write a property that a sorting function must satisfy for any input list
- Explain what shrinking does to a failing case and why it helps
- Say which faults example tests catch that property tests usually miss
1 · Read
You already know the test levels and where each belongs. Property tests add a new style. Instead of listing examples, you state one rule that must hold for every input, and the machine generates hundreds of inputs trying to break it.
Take sorting. A good property says the output is ordered and holds the same elements as the input. Another classic says decoding an encoded value returns the original. A parser and its printer must satisfy the same kind of round-trip rule.
When an input breaks the rule, shrinking kicks in. It trims the failing input down to the smallest one that still fails. A tiny failing case points straight at the cause, while the original thousand-element monster hides it.
Example tests still matter. They pin down exact outputs and concrete business rules that a general property cannot see. Use properties to hunt surprises and examples to lock behaviour in.
State a rule for every input, let generated inputs attack it, and shrink failures to their smallest form.
2 · Watch
Take it off screen
Where it sits
Learn first
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.