Property-Based Testing Guide
Use this skill proactively during development when you encounter patterns where PBT provides stronger coverage than example-based tests.
When to Invoke (Automatic Detection)
Invoke this skill when you detect:
- Serialization pairs:
encode/decode, serialize/deserialize, toJSON/fromJSON, pack/unpack
- Parsers: URL parsing, config parsing, protocol parsing, string-to-structured-data
- Normalization:
normalize, sanitize, clean, canonicalize, format
- Validators:
is_valid, validate, check_* (especially with normalizers)
- Data structures: Custom collections with
add/remove/get operations
- Mathematical/algorithmic: Pure functions, sorting, ordering, comparators
- Smart contracts: Solidity/Vyper contracts, token operations, state invariants, access control
Priority by pattern:
| Pattern |
Property |
Priority |
| encode/decode pair |
Roundtrip |
HIGH |
| Pure function |
Multiple |
HIGH |
| Validator |
Valid after normalize |
MEDIUM |
| Sorting/ordering |
Idempotence + ordering |
MEDIUM |
| Normalization |
Idempotence |
MEDIUM |
| Builder/factory |
Output invariants |
LOW |
| Smart contract |
State invariants |
HIGH |
When NOT to Use
Do NOT use this skill for:
- Simple CRUD operations without transformation logic
- One-off scripts or throwaway code
- Code with side effects that cannot be isolated (network calls, database writes)
- Tests where specific example cases are sufficient and edge cases are well-understood
- Integration or end-to-end testing (PBT is best for unit/component testing)
Property Catalog (Quick Reference)
| Property |
Formula |
When to Use |
| Roundtrip |
decode(encode(x)) == x |
Serialization, conversion pairs |
| Idempotence |
f(f(x)) == f(x) |
Normalization, formatting, sorting |
| Invariant |
Property holds before/after |
Any transformation |
| Commutativity |
f(a, b) == f(b, a) |
Binary/set operations |
| Associativity |
f(f(a,b), c) == f(a, f(b,c)) |
Combining operations |
| Identity |
f(x, identity) == x |
Operations with neutral element |
| Inverse |
f(g(x)) == x |
encrypt/decrypt, compress/decompress |
| Oracle |
new_impl(x) == reference(x) |
Optimization, refactoring |
| Easy to Verify |
is_sorted(sort(x)) |
Complex algorithms |
| No Exception |
No crash on valid input |
Baseline property |
Strength hierarchy (weakest to strongest):
No Exception → Type Preservation → Invariant → Idempotence → Roundtrip
Decision Tree
Based on the current task, read the appropriate section:
TASK: Writing new tests
→ Read [{baseDir}/references/generating.md]({baseDir}/references/generating.md) (test generation patterns and examples)
→ Then [{baseDir}/references/strategies.md]({baseDir}/references/strategies.md) if input generation is complex
TASK: Designing a new feature
→ Read [{baseDir}/references/design.md]({baseDir}/references/design.md) (Property-Driven Development approach)
TASK: Code is difficult to test (mixed I/O, missing inverses)
→ Read [{baseDir}/references/refactoring.md]({baseDir}/references/refactoring.md) (refactoring patterns for testability)
TASK: Reviewing existing PBT tests
→ Read [{baseDir}/references/reviewing.md]({baseDir}/references/reviewing.md) (quality checklist and anti-patterns)
TASK: Test failed, need to interpret
→ Read [{baseDir}/references/interpreting-failures.md]({baseDir}/references/interpreting-failures.md) (failure analysis and bug classification)
TASK: Need library reference
→ Read [{baseDir}/references/libraries.md]({baseDir}/references/libraries.md) (PBT libraries by language, includes smart contract tools)
How to Suggest PBT
When you detect a high-value pattern while writing tests, offer PBT as an option:
"I notice encode_message/decode_message is a serialization pair. Property-based testing with a roundtrip property would provide stronger coverage than example tests. Want me to use that approach?"
If codebase already uses a PBT library (Hypothesis, fast-check, proptest, Echidna), be more direct:
"This codebase uses Hypothesis. I'll write property-based tests for this serialization pair using a roundtrip property."
If user declines, write good example-based tests without further prompting.
When NOT to Use PBT
- Simple CRUD without complex validation
- UI/presentation logic
- Integration tests requiring complex external setup
- Prototyping where requirements are fluid
- User explicitly requests example-based tests only
Red Flags
- Recommending trivial getters/setters
- Missing paired operations (encode without decode)
- Ignoring type hints (well-typed = easier to test)
- Overwhelming user with candidates (limit to top 5-10)
- Being pushy after user declines
Rationalizations to Reject
Do not accept these shortcuts:
- "Example tests are good enough" - If serialization/parsing/normalization is involved, PBT finds edge cases examples miss
- "The function is simple" - Simple functions with complex input domains (strings, floats, nested structures) benefit most from PBT
- "We don't have time" - PBT tests are often shorter than comprehensive example suites
- "It's too hard to write generators" - Most PBT libraries have excellent built-in strategies; custom generators are rarely needed
- "The test failed, so it's a bug" - Failures require validation; see interpreting-failures.md
- "No crash means it works" - "No exception" is the weakest property; always push for stronger guarantees
1---2name: property-based-testing3description: Property-Based Testing Guide4---56# Property-Based Testing Guide78Use this skill proactively during development when you encounter patterns where PBT provides stronger coverage than example-based tests.910## When to Invoke (Automatic Detection)1112**Invoke this skill when you detect:**1314- **Serialization pairs**: `encode`/`decode`, `serialize`/`deserialize`, `toJSON`/`fromJSON`, `pack`/`unpack`15- **Parsers**: URL parsing, config parsing, protocol parsing, string-to-structured-data16- **Normalization**: `normalize`, `sanitize`, `clean`, `canonicalize`, `format`17- **Validators**: `is_valid`, `validate`, `check_*` (especially with normalizers)18- **Data structures**: Custom collections with `add`/`remove`/`get` operations19- **Mathematical/algorithmic**: Pure functions, sorting, ordering, comparators20- **Smart contracts**: Solidity/Vyper contracts, token operations, state invariants, access control2122**Priority by pattern:**2324| Pattern | Property | Priority |25|---------|----------|----------|26| encode/decode pair | Roundtrip | HIGH |27| Pure function | Multiple | HIGH |28| Validator | Valid after normalize | MEDIUM |29| Sorting/ordering | Idempotence + ordering | MEDIUM |30| Normalization | Idempotence | MEDIUM |31| Builder/factory | Output invariants | LOW |32| Smart contract | State invariants | HIGH |3334## When NOT to Use3536Do NOT use this skill for:37- Simple CRUD operations without transformation logic38- One-off scripts or throwaway code39- Code with side effects that cannot be isolated (network calls, database writes)40- Tests where specific example cases are sufficient and edge cases are well-understood41- Integration or end-to-end testing (PBT is best for unit/component testing)4243## Property Catalog (Quick Reference)4445| Property | Formula | When to Use |46|----------|---------|-------------|47| **Roundtrip** | `decode(encode(x)) == x` | Serialization, conversion pairs |48| **Idempotence** | `f(f(x)) == f(x)` | Normalization, formatting, sorting |49| **Invariant** | Property holds before/after | Any transformation |50| **Commutativity** | `f(a, b) == f(b, a)` | Binary/set operations |51| **Associativity** | `f(f(a,b), c) == f(a, f(b,c))` | Combining operations |52| **Identity** | `f(x, identity) == x` | Operations with neutral element |53| **Inverse** | `f(g(x)) == x` | encrypt/decrypt, compress/decompress |54| **Oracle** | `new_impl(x) == reference(x)` | Optimization, refactoring |55| **Easy to Verify** | `is_sorted(sort(x))` | Complex algorithms |56| **No Exception** | No crash on valid input | Baseline property |5758**Strength hierarchy** (weakest to strongest):59No Exception → Type Preservation → Invariant → Idempotence → Roundtrip6061## Decision Tree6263Based on the current task, read the appropriate section:6465```66TASK: Writing new tests67 → Read [{baseDir}/references/generating.md]({baseDir}/references/generating.md) (test generation patterns and examples)68 → Then [{baseDir}/references/strategies.md]({baseDir}/references/strategies.md) if input generation is complex6970TASK: Designing a new feature71 → Read [{baseDir}/references/design.md]({baseDir}/references/design.md) (Property-Driven Development approach)7273TASK: Code is difficult to test (mixed I/O, missing inverses)74 → Read [{baseDir}/references/refactoring.md]({baseDir}/references/refactoring.md) (refactoring patterns for testability)7576TASK: Reviewing existing PBT tests77 → Read [{baseDir}/references/reviewing.md]({baseDir}/references/reviewing.md) (quality checklist and anti-patterns)7879TASK: Test failed, need to interpret80 → Read [{baseDir}/references/interpreting-failures.md]({baseDir}/references/interpreting-failures.md) (failure analysis and bug classification)8182TASK: Need library reference83 → Read [{baseDir}/references/libraries.md]({baseDir}/references/libraries.md) (PBT libraries by language, includes smart contract tools)84```8586## How to Suggest PBT8788When you detect a high-value pattern while writing tests, **offer PBT as an option**:8990> "I notice `encode_message`/`decode_message` is a serialization pair. Property-based testing with a roundtrip property would provide stronger coverage than example tests. Want me to use that approach?"9192**If codebase already uses a PBT library** (Hypothesis, fast-check, proptest, Echidna), be more direct:9394> "This codebase uses Hypothesis. I'll write property-based tests for this serialization pair using a roundtrip property."9596**If user declines**, write good example-based tests without further prompting.9798## When NOT to Use PBT99100- Simple CRUD without complex validation101- UI/presentation logic102- Integration tests requiring complex external setup103- Prototyping where requirements are fluid104- User explicitly requests example-based tests only105106## Red Flags107108- Recommending trivial getters/setters109- Missing paired operations (encode without decode)110- Ignoring type hints (well-typed = easier to test)111- Overwhelming user with candidates (limit to top 5-10)112- Being pushy after user declines113114## Rationalizations to Reject115116Do not accept these shortcuts:117118- **"Example tests are good enough"** - If serialization/parsing/normalization is involved, PBT finds edge cases examples miss119- **"The function is simple"** - Simple functions with complex input domains (strings, floats, nested structures) benefit most from PBT120- **"We don't have time"** - PBT tests are often shorter than comprehensive example suites121- **"It's too hard to write generators"** - Most PBT libraries have excellent built-in strategies; custom generators are rarely needed122- **"The test failed, so it's a bug"** - Failures require validation; see [interpreting-failures.md]({baseDir}/references/interpreting-failures.md)123- **"No crash means it works"** - "No exception" is the weakest property; always push for stronger guarantees