What is property-based testing?
Answer
Property-based testing (PBT) generates random input data and checks that certain properties/invariants always hold, rather than testing specific example inputs. Instead of writing: "given input 5, expect output 25" (example-based), write: "for any non-negative integer n, square(n) should be >= n." The PBT framework generates hundreds of random inputs and runs each through the property. When a failure is found, it shrinks the input to the minimal failing case. Tools: QuickCheck (Haskell — the original), fast-check (JavaScript), Hypothesis (Python — excellent), FsCheck (F#/.NET), jqwik (Java). Properties to test: commutativity (a+b = b+a), associativity, idempotence (applying twice = applying once), inverse (encode then decode = identity), round-trips (serialize then deserialize = original). PBT finds edge cases humans never think to test — it discovered the infamous "equal adjacent elements" bug in sorting algorithms that passed all hand-written tests.