Property Based Testing

Use when: test invariants over generated inputs instead of a few hand-picked examples.

kimtth 88c2e79 1014 B Updated

File contents

Goal: find edge cases by checking properties across many inputs.

Use for:

  • logic with broad input ranges or many edge cases
  • parsers, serializers, and data transformations
  • complementing example-based tests

Workflow:

  1. Identify an invariant that must always hold.
  2. Define generators for the relevant input space.
  3. Let the framework run many randomized cases.
  4. Let it shrink failures to a minimal counterexample.
  5. Add the counterexample as a regression test.
  6. Refine generators to cover meaningful edges.

Useful properties:

  • round-trip: decode(encode(x)) == x
  • invariant: output always satisfies a rule
  • equivalence: two implementations agree
  • idempotence: f(f(x)) == f(x)

Rules:

  • assert properties, not specific outputs
  • constrain generators to valid, meaningful inputs
  • pin discovered counterexamples as fixed tests
  • use seeds so failures reproduce

kimtth/agent-skill-100-lines-or-less/tree/main/skills/property-based-testing commit 88c2e79685

Frequently asked questions

npx skillmds@latest add kimtth/property-based-testing