PROPERTY-BASED TESTING
State what must always hold, then let the machine hunt for the input that breaks it
split(total, n) shares an amount between n people. Your property says: the shares add up to the total. The buggy version gives the whole remainder to the first person, so split(10, 4) returns [4, 2, 2, 2]. Does the property catch it?
An example test says: for this input, expect this output. You choose the input, so you cover what you thought of. A property says what must be true of every output, and the tool draws the inputs: empty, zero, one, duplicates, negatives, huge, unicode.
When a generated input fails, it is usually long and noisy. The tool shrinks it: it tries smaller inputs and keeps each one that still fails, until nothing smaller does. A saved seed or failing example makes the failure replay. That is the loop in Hypothesis (Python), fast-check (JavaScript) and QuickCheck (Haskell, where the idea began).
// example tests: every amount divides evenly
expect(split(100, 4)).toEqual([25, 25, 25, 25]);
expect(split(90, 3)).toEqual([30, 30, 30]);
function split(total, n) {
if (total < n) return []; // never reached by the examples
...
}// a property holds for every input, not one you picked
fc.assert(fc.property(fc.nat(10_000_000), fc.integer({ min: 1, max: 12 }), (total, n) => {
const s = split(total, n);
return s.length === n
&& s.every(Number.isInteger)
&& s.reduce((a, b) => a + b, 0) === total
&& Math.max(...s) - Math.min(...s) <= 1;
})); // on a failure the library shrinks to the smallest input Follow the build and the invariant strip. The examples pass, the generated inputs do not, and then each shrink step keeps a failing input and makes it smaller.
The runner draws inputs from a seed. With 2 inputs and seed 1 the property holds on every one and the build is green. Change the seed and the number of generated inputs until the runner finds a counterexample, then watch it shrink.
GOAL: the runner finds a counterexample
Write property(total, n, shares), given the amount in whole kobo, the number of people (n ≥ 1) and the array that split returned. Return true when the result is a fair split: exactly n shares, each a whole non-negative number, adding back to total, and no two shares more than 1 kobo apart. The harness checks that it holds on the correct split for 300 seeded inputs (including 0, 1 person, and amounts smaller than n) and fails on each of six buggy versions.
A property is only as strong as the rules it states. This is the same lesson as boundary values turned around: there you choose the edges, here the generator is allowed to find them. Generated inputs also need a seed or saved example for the same reason a race needs a schedule: a failure you cannot replay is a failure you cannot fix.
Sorting, deduplication, merging and parsing (decode(encode(x)) equals x), money and quantity splits, serialisers, caches and state machines, anywhere the input space is too big to list. Hypothesis, fast-check and QuickCheck all ship generators and shrinkers. Keep the run count and the seed or failing example in CI output so a red build replays.
“What properties would you test for a sort function?” The output is ordered, it has the same elements as the input (same length, same multiset), and sorting it again changes nothing. Then say what generator you would use (empty, one element, duplicates, negatives) and why a test that only checks ordering would pass a function that returns an empty list.
USED IN
Chapters that build on this lesson.
- Project: put a legacy function under test and find its bugSoftware Testing and Quality
- Property-based testing: say what is always true, let the machine huntSoftware Testing and Quality
- Security testing: fuzzing, scanners and abuse casesSoftware Testing and Quality
- Testing money: invariants, reconciliation and evidence for a regulatorSoftware Testing and Quality