Skip to content

Property-Based Testing

Spicy — senior dev territoryTesting

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

Made with passive-aggressive love by manoga.digital. Powered by Claude.