Shaf Lab

Property testing

Checking that a property holds for many generated inputs, not just hand-written cases.

Property testing checks that a rule holds for many generated inputs, not just the examples you wrote by hand. You state a property — parsing then printing returns the original document, or a sort never drops an item — and a generator produces cases until it finds a counterexample, often shrunk down to a small one. It sits between unit tests and a full formal proof.

Use it on parsers, codecs, data structures, and any function with a clear invariant. It is a weaker fit for UI flows or for code that talks to the network, unless you can isolate the logic. A good property is short and falsifiable. A vague property will generate a lot of noise and no insight.

When a case fails, keep the shrunk input in a regular unit test so it cannot regress. Watch the generator too: if it never produces nested or empty inputs, you are not testing the property you think you are.

Property testing will not prove the code is correct in every possible case. It will find the cases you did not think to write. That is usually enough to earn a place next to the hand-written tests.