PROPERTY-BASED TESTING

State what must always hold, then let the machine hunt for the input that breaks it

L216 MINTESTING
PREDICT

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).

▶ SIMULATION
CODE UNDER TEST
split(): returns [] when total < people
property: n shares, whole kobo, sum = total, spread at most 1
THE BUILD
examples: 0 · generated: 0
running
✓ the build is red whenever split() can return a wrong answer✓ the build is green when split() is right
SCHEDULE
01[CODE]split(): returns [] when there are more people than kobo
02[SUITE]example test: split(100, 4) adds back up
03[SUITE]example test: split(90, 3) adds back up
04[SUITE]example test: split(1000, 5) adds back up
05[RUNNER]runner: report the build
06[RELEASE]release: the build is green, so ship it
Start of examples only scenario. 6 steps.
// 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
  ...
}

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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

Write down what must always hold, not what happens for one input. Let a generator draw the inputs, let shrinking reduce the failure to a minimal case, and keep the seed or saved example so it replays. A weak property passes bad code, so check the property itself against buggy versions.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: TIME IN TESTS →