Phase 3: Property-Based Testing for AI Code · 50 min · Python · Hypothesis · pytest
The Problem
An AI generates a sort_list function:
def sort_list(items):
return sorted(items)
You write 3 example-based tests:
def test_sort_basic():
assert sort_list([3, 1, 2]) == [1, 2, 3]
def test_sort_empty():
assert sort_list([]) == []
def test_sort_single():
assert sort_list([1]) == [1]
All pass. But what about [3, 1, 2, 1] (duplicates)? [-1, 0, 1] (negatives)? [3, -1, float('inf'), 0] (mixed types)? [1] * 10000 (large input)? Example-based tests only check the specific inputs you thought of. AI code has edge cases you didn't think of.
Property-based testing flips the approach: instead of specifying individual inputs and expected outputs, you specify invariants -- properties that must hold for all inputs -- and the testing framework generates hundreds of random inputs to try to break them.
Example-based vs property-based testing
| Aspect | Example-Based | Property-Based |
|---|
| What you specify | Input + expected output | Invariant (property) that must hold |
| Number of test cases | 3-10 (what you thought of) | 100-1000 (randomly generated) |
| Edge case coverage | Only edges you considered | Framework finds edges you missed |
| AI code relevance | Misses AI-specific edge cases | Catches AI logic errors at scale |
| Failure reporting | "test_sort_basic failed" | Minimal counterexample (shrunk) |
| Maintenance | Add new test for each edge case | Property covers all cases automatically |
Example: Sorting
# Example-based: 3 test cases
def test_sort_basic():
assert sort_list([3, 1, 2]) == [1, 2, 3]
# Property-based: 1 property, 200+ test cases
@given(st.lists(st.integers()))
def test_sort_produces_sorted_output(items):
result = sort_list(items)
assert result == sorted(result) # invariant: output is sorted
@given(st.lists(st.integers()))
def test_sort_preserves_length(items):
result = sort_list(items)
assert len(result) == len(items) # invariant: length preserved
@given(st.lists(st.integers()))
def test_sort_preserves_elements(items):
result = sort_list(items)
assert sorted(result) == sorted(items) # invariant: same elements
PBT catches what example tests miss
The AI generates a sort function that works for [3,1,2] but fails for duplicates [3,1,2,1] (it removes duplicates). Example tests with [3,1,2] pass. But the property "sort preserves elements" fails for [3,1,2,1] because the result [1,2,3] is missing the duplicate 1. Hypothesis finds this edge case automatically by generating random lists including ones with duplicates. The property catches the AI logic error that no example test covered.
History: QuickCheck to hypothesis
| Year | Milestone | Significance |
|---|
| 2000 | QuickCheck (Haskell) | Claessen & Hughes introduce PBT for Haskell |
| 2010s | QuickCheck ports | Ports to Erlang, Scala, Java, JavaScript |
| 2019 | Hypothesis (Python) | MacIver et al. bring PBT to Python with shrinking |
| 2024 | PBT-Bench | Benchmark for evaluating AI agents on PBT tasks |
| 2025 | Anthropic agentic PBT | Claude Code's /hypothesis command, 56% valid bug rate |
Why PBT matters for AI code specifically
AI code has a unique defect pattern: it works for common cases but fails for edge cases the AI didn't "think of." This is because:
- Training data bias: AI learns from common patterns, not edge cases
- No mental model: AI doesn't have a mental model of the function's behavior
- Pattern matching, not reasoning: AI matches patterns from training data, not logical invariants
PBT addresses this by generating inputs the AI didn't consider, including edge cases, boundary values, and adversarial inputs.
An AI generates a deduplicate function that removes duplicates from a list. You write 3 example tests: assert deduplicate([1,1,2]) == [1,2], assert deduplicate([]) == [], assert deduplicate([1]) == [1]. All pass. What property would catch the bug where the AI's implementation also sorts the output (changing order)?
The property 'deduplicate preserves first occurrence order' catches the bug. For any input list, the output should preserve the order in which elements first appear. The AI's implementation sorts the output, so deduplicate([3,1,3,2]) returns [1,2,3] (sorted) instead of [3,1,2] (first-occurrence order). Example tests with [1,1,2] pass because the sorted order [1,2] happens to match the first-occurrence order [1,2]. But Hypothesis generates random lists like [3,1,3,2] where the sorted order differs from first-occurrence order, and the property fails. This is the core advantage of PBT: it generates inputs that expose edge cases the example tests miss because the test author didn't think of them."