Section 6 · Lesson 3
Property-Based Testing — Prove the Rules
Go beyond example tests — define system invariants from EARS requirements, let counter-example engines and shrinking find the bugs you would never write by hand.
"What are your system's non-negotiable truths — and are you ready to prove them?
Beyond a handful of examples
You write five or ten tests — happy path, a few edge cases. But thousands of weird inputs you would never try can still break production. Property-based testing asks: what if tests could stress all valid inputs, not just the ones you thought of?
Coverage is not correctness
100% coverage and green CI can still ship critical bugs. Coverage only proves a line ran — not that behavior is correct for every input. Example-based testing is spot-checking; you hope the rest holds.
Properties, not spot checks
State a universal rule — a property — that must hold for any valid input. Stop asking “does this work for this input?” and ask “what fundamental rules must never break?” That is core intent, not just implementation detail.
Outsource the hunt for corner cases
Define the rule, then unleash a framework that tries to prove you wrong with counterexamples. Empty strings, emoji, control characters, huge payloads, numeric limits — thousands of variations hunting for one failure.
EARS → system invariant
Easy Approach to Requirements Syntax bridges vague English to testable logic: When some condition, then the system shall do a specific thing. No room for “fast” or “easy to use” — map each requirement to an invariant that is always true.
Clear spec → invariant → executable property
Start with a precise spec sentence. Derive a system invariant (e.g. for any unauthenticated request, status must be 401). Encode that invariant as an executable property your code must obey.
Shrinking
When a counterexample is found — even a huge JSON blob — modern PBT shrinks it to the simplest input that still fails. You go from haystack to needle: order quantity exactly zero, not an accidental complex payload.
Prove non-negotiable truths
This is not about a few more bugs — it is confidence that the system behaves as designed. Ask: what are your non-negotiable truths? Are you ready to prove them in CI?
Example-based
Example-based: “Does this code work for this specific input I thought of?” — spot-checking; coverage can lie.
Property-based
Property-based: “What rules must never break for any valid input?” — a promise the machine tries to falsify.
EARS → system invariant
| Theme | EARS requirement | Invariant |
|---|---|---|
| Security | When the request is unauthenticated, the system shall return status 401. | For any unauthenticated request, the response status must be 401. |
| Validation | When order quantity is submitted, the system shall reject non-positive values. | For any order payload, quantity must be greater than zero or the request is rejected. |
| Business rule | When cart totals are computed, the system shall never return a negative total. | For any valid cart state, computed total is greater than or equal to zero. |
Counter-example found
Rule: Every order must have quantity greater than zero.
Counterexample: quantity = 0 slips through validation — example-based tests never tried it.
Shrinking reduces a messy failing payload to the minimal case: quantity 0.
Six-step recipe
- 1
Write acceptance criteria in EARS
Precise when/then sentences in the spec — no fuzzy adjectives.
- 2
Group by theme → invariants
Cluster security, validation, and business rules into core system invariants.
- 3
Define generators
Tell the framework how to produce valid random data (API payloads, users, carts).
- 4
Write the property in code
Encode invariants with Hypothesis (Python) or FastCheck (JavaScript/TypeScript).
- 5
Enable shrinking and replay
Failures shrink to minimal inputs and reproduce reliably for debugging.
- 6
Mandatory CI gate
Block merges when properties fail — fundamental truths are non-negotiable.
Frameworks
Hypothesis
Python
Gold standard for property-based tests in Python ecosystems.
FastCheck
JavaScript / TypeScript
Property testing for Node and browser test runners.
Practice
Check your understanding
A few minutes of recall now saves rereading later.
1.100% line coverage tells you that…
2.What does shrinking do after a property fails?
3.Which framework is the common choice for TypeScript?
Order the six-step property-based testing recipe. Use the arrows to move a step, then check your order.
- 1Write acceptance criteria in EARS
- 2Enable shrinking and replay
- 3Write the property in code
- 4Group by theme → invariants
- 5Define generators
- 6Mandatory CI gate