property-based-testing-with-kotestlisted
Install: claude install-skill msewell/agent-stuff
# Property-Based Testing with Kotest
## Workflow: Writing a property-based test
1. **Decide if PBT fits.** Use PBT for functions with broad input spaces, clear
invariants, round-trip operations, or many parameter combinations. Keep
example-based tests for specific business scenarios and regression pinning.
2. **Identify properties.** Pick from these patterns (most to least common):
- **Roundtrip** — `decode(encode(x)) == x`. For serialize/parse/compress pairs.
- **Invariant** — a measurable property is preserved (e.g., `sort` preserves size and elements).
- **Idempotence** — `f(f(x)) == f(x)`. For trim, distinct, upsert, PUT.
- **Oracle** — compare optimized implementation against a simple correct one.
- **Hard to prove, easy to verify** — check output validity (e.g., prime factors multiply back).
- **Commutativity** — different operation orders yield same result.
- **Induction** — base case + recursive step.
- **Metamorphic** — when correct output is unknown, test relationships between outputs under related inputs.
Always identify **at least two** complementary properties per function under test.
3. **Design generators.** Use `Arb` for random+edge-case generation (default),
`Exhaustive` for small finite domains. Constrain generators at construction
— don't rely on `filter()` or `assume()` (keep discard rate under 10%).
For domain types, compose with `arbitrary { ... }` using `.bind()`.
4. **Write the test.** Use `checkAll` with Kot