Property-based testing
P30.property-based.01 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Fixtures & parametrisation
Where next?
Go up a level
P30.property-based.01 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Fixtures & parametrisation
Go up a level
Example tests check the inputs you thought of. The bugs live in the ones you
did not - the empty list, the run of ten, the string with a digit already in
it. Property-based testing flips the burden: you state an invariant that
should hold for every input, a generator invents hundreds of inputs, and when
one breaks the property it shrinks that input to the smallest version that
still fails. hypothesis is the production tool; this module builds its beating
heart - generate, check, shrink - in a few lines each.
decode(encode(s)) == s, sorted(xs) is ordered and a permutation of xs, reverse(reverse(xs)) == xs. You assert the rule once; the framework supplies the inputs. It is parametrisation with the table generated for you.Why it matters — Parametrisation was you listing cases; a property is the machine listing them - it reaches the empty, the huge, and the weird inputs your imagination skips, which is exactly where bugs hide.
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Your turn: write for_all to find a counterexample, then
shrink to minimise it, argue property vs example, and close with
the interview on deriving properties for merge().
Loading exercise…
Loading exercise…
Loading exercise…
Loading exercise…
Property-based testing is not a replacement for examples - it is a searchlight
for the inputs you would never think to write down. Build the generator and the
shrinker once, and @given stops being magic.
Loading your notes...