Property-Based Testing
On this page
- What is Property Testing?
- When to Use Property Testing
- Kimberlite Property Tests
- 1. Offset Monotonicity
- 2. Hash Chain Integrity
- 3. State Machine Commutativity
- 4. Serialization Round-Trip
- 5. Encryption/Decryption Symmetry
- Custom Generators
- Shrinking
- Configuration
- Regression Tests
- Performance Tips
- 1. Use Smaller Ranges
- 2. Filter Early
- 3. Parallel Execution
- Common Patterns
- Invariant Testing
- Round-Trip Testing
- Comparison Testing
- Related Documentation
Test invariants using proptest to find edge cases automatically.
What is Property Testing?
Property testing generates hundreds of random test cases to verify that invariants (“properties”) hold for all inputs.
Example invariant: “Offset must increase monotonically”
// Instead of testing specific cases:
assert_eq!;
assert_eq!;
// Test the property for all possible commands:
proptest!
When to Use Property Testing
Use property tests for:
- Invariants - Properties that must always hold
- Symmetry -
encode(decode(x)) == x - Idempotence -
f(f(x)) == f(x) - Commutativity -
a + b == b + a - Associativity -
(a + b) + c == a + (b + c)
Don’t use for:
- Specific business logic (use unit tests)
- Integration tests (use integration tests)
- Fuzz testing parsers (use fuzzing)
Kimberlite Property Tests
1. Offset Monotonicity
use *;
use *;
proptest!
2. Hash Chain Integrity
proptest!
3. State Machine Commutativity
proptest!
4. Serialization Round-Trip
proptest!
5. Encryption/Decryption Symmetry
proptest!
Custom Generators
Create custom generators for domain types:
use *;
// Strategy for TenantId (1-1000)
// Strategy for valid Commands
// Use custom strategy
proptest!
Shrinking
When a property test fails, proptest automatically shrinks the input to the minimal failing case:
proptest!
Shrinking output:
thread 'test_parse_fails_on_invalid' panicked at 'Test failed after 23 iterations.
Minimal failing input: "("
Configuration
Control proptest behavior:
proptest!
Or via environment variable:
PROPTEST_CASES=10000
Regression Tests
Proptest saves failing cases to proptest-regressions/:
These are automatically re-run on every test to prevent regressions.
Performance Tips
1. Use Smaller Ranges
// ❌ Slow: Generates huge collections
vec
// ✅ Fast: Reasonable size
vec
2. Filter Early
// ❌ Slow: Generates then filters
.prop_filter
// ✅ Fast: Generate only evens
.prop_map
3. Parallel Execution
# Run property tests in parallel
Common Patterns
Invariant Testing
proptest!
Round-Trip Testing
proptest!
Comparison Testing
proptest!
Related Documentation
- Testing Overview - General testing philosophy
- Assertions Guide - Runtime assertion patterns
- Testing Strategy (Internal) - Detailed testing approach
Key Takeaway: Property tests find edge cases you wouldn’t think to test manually. Use them for invariants, symmetry, and round-trips. Proptest will shrink failures to minimal cases.