Skip to content

skillTesting

Property Based Testing

Writes property-based tests with generators, invariants and shrinking for parsers, serializers and pure logic where example tests miss edge cases.

npx workspacetemplates add property-based-testing --tool claude-code

Writes 1 file to .claude/skills/property-based-testing/

.claude/skills/property-based-testing/SKILL.md31 lines · 1.6 KBFile
---
name: property-based-testing
description: Writes property-based tests with generators, invariants and shrinking for parsers, serializers and pure logic where example tests miss edge cases.
---

# Property based testing

Find a rule that must always hold, then let a generator attack it.

## Steps

1. Pick the library already in the project, or the standard one for the language (fast-check, Hypothesis, jqwik, gopter or rapid, proptest). Do not add a second.
2. Write the property in one plain sentence before any code. Good shapes:
   - round trip: `decode(encode(x)) == x`
   - idempotence: `normalize(normalize(x)) == normalize(x)`
   - invariant: output length, sum or sort order is preserved
   - oracle: the fast version equals a naive, obviously correct version
   - no crash: arbitrary input returns a value or a documented error, never an unhandled exception
3. Build generators for the domain, not raw strings: valid records, boundary numbers (0, -1, max, NaN), unicode, empty and huge collections. Constrain with filters sparingly, since heavy filtering wastes runs.
4. Run at least 200 cases locally and more in CI. Keep the seed printed on failure.
5. When a property fails, use the shrunk counterexample: decide whether the code or the property is wrong, fix it, and keep the counterexample as a plain example test.

## Rules

- A property must not recompute the implementation. That proves nothing.
- Never swallow the library's seed or disable shrinking.
- Keep each property to one rule.

## Report

The properties added, the generators, any bug found with its shrunk input, and the run count used.

What it does

Example tests check the inputs you thought of. Property tests check a rule across thousands of generated inputs and hand back the smallest input that breaks it. This skill teaches the agent to find the rule first, then pick a library and write the generator.

It focuses on properties that pay off: round trips, idempotence, ordering and conservation laws, plus comparison against a slow but obviously correct model.

Example

you › Add property tests for parseDuration() in src/time.ts

agent › Added 3 properties with fast-check: format(parse(s)) round-trips for valid input, parse never throws on arbitrary strings, and parse("90m") equals parse("1h30m"). Found a bug: parse("0.5h") returned 0. Shrunk input: "0.5h". Fixed in parseFloat handling and kept it as a named example test.

Questions

How is this different from fuzzing?
Fuzzing hunts for crashes with random bytes. Property tests state a rule about correct output and shrink failures to a readable minimal case, so they fit business logic too.
Will it make my test suite slow?
Each property runs a few hundred cases in well under a second for pure code. The agent lowers the count for anything touching I/O.
  • Contract Testing

    SkillTesting

    Free
  • E2E Test Design

    SkillTesting

    Free
  • Flaky Test Diagnosis

    SkillTesting

    Free