Property-Based Testing
Simple Definition
Property-based testing is a way of testing software where you state a rule that should always be true, called a property, and a testing library checks it against hundreds or thousands of automatically generated inputs. When an input breaks the rule, the library shrinks it to the smallest version that still fails, so you can see the bug clearly.
How It Differs From Example-Based Tests
A normal unit test checks one example you thought of: “a deposit of 100 into an empty account gives a balance of 100.” A property states something general: “for any list of valid deposits and withdrawals, the balance never goes below zero, and it always equals deposits minus withdrawals.” The library then invents the inputs: empty lists, huge amounts, a withdrawal of exactly the full balance, thousands of tiny transactions, values with awkward decimals.
The value is in the cases you did not think of. People write examples they expect to pass; a generator has no such bias.
The Key Ideas
- Properties and invariants. An invariant is a rule that must hold no matter what happens, such as “the balance is never negative” or “sorting a list twice gives the same result as sorting it once.” Other common properties: encoding then decoding returns the original, or the output has the same length as the input.
- Generators. You describe the shape of valid input (numbers within a range, strings, lists of transactions), and the library produces random examples, deliberately including edge cases like zero, empty values, and limits.
- Shrinking. A failing input found at random is often large and messy. The library simplifies it step by step, so a failure on a list of 200 transactions might shrink to two: deposit 0.1, withdraw 0.1.
- Seeds and reproducibility. Generated inputs come from a random seed. Good libraries print the seed (or save the failing case) so you can rerun exactly the same test. Without that, a failure you saw once may be impossible to reproduce.
Popular libraries include Hypothesis for Python, fast-check for JavaScript and TypeScript, and QuickCheck for Haskell, which popularized the idea.
Example
A banking rule says an account can never be overdrawn. You write one property: “for any sequence of transactions the system accepts, the final balance is at least zero.” The generator tries thousands of sequences and finds a failure: two withdrawals processed in the same millisecond each saw the old balance. Shrinking reduces it to a two-step case that makes the race condition obvious. Ten hand-written examples would likely never have hit it.
Why It Matters for AI-Written Code
AI coding agents write plausible code quickly, and they also write the tests, often with examples that match what the code already does. A property states what must be true, independent of how the code was written, which makes it a stronger check on generated code. Asking an agent to propose properties for a function is also a useful way to surface what the agent thinks the function is for.
Limits
Property-based testing complements other tests rather than replacing them. It is only as good as the properties you write: a weak property (“the function returns something”) passes everything. It can be slow for expensive code, generators for complex inputs take effort to write, and randomness means a rare bug may not appear on every run. Keep example tests for known cases and regressions, and add properties for rules that must always hold.
Related Terms
- Differential Testing, comparing two implementations on the same inputs, often with generated inputs
- Prompt Regression Testing, the same “rerun fixed checks after every change” idea applied to prompts
- Verification Gate, a check work must pass before it counts as done
- AI Code Risk Review, reviewing AI-generated code for the risks tests miss
Continue learning
Explore related guides, tools, workflows, and prompts that help you go deeper into this topic.
Browse all AI terms.
Learn termSee these concepts in practice.
Open workflowA simple explanation of this AI concept.
Learn termA simple explanation of this AI concept.
Learn termA simple explanation of this AI concept.
Learn termA simple explanation of this AI concept.
Learn termSee AI terms in action
Browse practical AI workflows that use the concepts in this glossary.
Last updated: