Blog
Property-Based Testing: Stop Hand-Picking Polite Inputs
Three example tests often describe three memories the developer happened to have. A property describes what must remain true across a space of inputs.
State The Invariant
For sorting: output is ordered and contains the same multiset. For serialization: decoding an encoded valid value returns the original. For money allocation: parts plus remainder equal total.
@Property
void allocationPreservesTotal(@ForAll @Positive int cents,
@ForAll @IntRange(min = 1, max = 100) int parts) {
Allocation a = allocate(cents, parts);
assertThat(a.items().stream().mapToInt(Integer::intValue).sum())
.isEqualTo(cents);
}
Generators Are Domain Models
Generate valid and invalid values deliberately. Random strings rarely exercise currency scale, Unicode normalization, or date transitions usefully.
Good tools shrink a failing case to the smallest counterexample. Keep the seed in test output so the failure reproduces.
Limit generators according to the contract, not according to what is convenient for the implementation. If a value may contain any valid Unicode, an ASCII-only generator protects a false assumption. Conversely, generating malformed objects through constructors that promise validity tests an impossible state and produces noise.
Property tests complement precise examples. When one counterexample reveals a regression, add a named example if it teaches future readers something important.