Phase 3: Property-Based Testing for AI Code · 55 min · Python · Hypothesis · pytest
Deriving Properties from Code
Read the code. Find the invariants. Write the properties.
Hiring signal: Engineers who can derive properties (roundtrip, monotonicity, bounded output, commutativity, identity) from code demonstrate the analytical skills needed to specify invariants for AI-generated functions.
What you will learn
- Derive roundtrip properties from serialization/deserialization functions
- Derive monotonicity properties from sorting and comparison functions
- Derive bounded output properties from mathematical functions
- Derive commutativity and identity properties from binary operations
- Use the Roundtrip/Model-oracle/Invariant property taxonomy
The Problem
You know how to write property tests with Hypothesis (Lesson 2). But what properties should you test? This is the hardest part of PBT: looking at a function and identifying the invariants it should satisfy. This lesson provides a taxonomy of property types and a systematic process for deriving them from any function.
The Property Taxonomy
| Property Type | What it checks | Example |
|---|
| Roundtrip | decode(encode(x)) == x | json.loads(json.dumps(d)) == d |
| Monotonicity | x <= y implies f(x) <= f(y) | sort preserves order |
| Bounded output | f(x) in [min, max] for all x | clamp(x, 0, 100) in [0, 100] |
| Commutativity | f(a, b) == f(b, a) | add(a, b) == add(b, a) |
| Identity | f(x, identity) == x | add(x, 0) == x |
| Model/Oracle | f(x) == reference_implementation(x) | Compare AI sort to sorted() |
| Idempotence | f(f(x)) == f(x) | deduplicate(deduplicate(x)) == deduplicate(x) |
| Associativity | f(f(a, b), c) == f(a, f(b, c)) | add(add(a, b), c) == add(a, add(b, c)) |
The 3-step property derivation process
- Read the function signature and docstring -- what does it claim to do? 2. Identify the output domain -- what values can the output take? 3. Ask: what must be true for ALL valid inputs? Write each as a property. Start with roundtrip (if serialization), then model/oracle (if a reference exists), then invariants (bounded output, monotonicity, etc.).
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers Roundtrip Properties, Monotonicity Properties, Bounded Output Properties, Model/Oracle Properties, Build It — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy