Property-Based Testing
ELI5 — The Vibe Check
Instead of writing specific test cases (add(2,3)=5), property-based testing generates thousands of random inputs automatically and checks that certain properties always hold true — like 'add is always commutative: add(a,b) always equals add(b,a)'. You write rules, the computer tests them at scale.
Real Talk
Property-based testing defines properties (invariants) that must hold for all valid inputs, then generates random test cases to try to falsify those properties. When a failure is found, it shrinks the input to the minimal failing case. Libraries: fast-check (JS), Hypothesis (Python), QuickCheck (Haskell).
Show Me The Code
import * as fc from 'fast-check';
test('sort is idempotent', () => {
fc.assert(
fc.property(fc.array(fc.integer()), (arr) => {
const sorted = arr.sort();
expect(sorted.sort()).toEqual(sorted);
})
);
});
When You'll Hear This
"Use property-based testing for the parser — we can't think of all the edge cases manually." / "fast-check found a crash with an empty string we never would have written a test for."
Related Terms
Edge Case
Edge cases are the weird, extreme, or unexpected inputs that trip up your code. What if someone types 0 for age?
Fuzzing
Fuzzing is throwing completely random, malformed, or garbage inputs at your program to see if it crashes.
Mutation Testing
Mutation testing is a way to test your tests.
Unit Test
A unit test is like checking that one single LEGO brick isn't broken before you use it in your big castle.