Property-based testing verifies that code satisfies general properties or invariants for a wide range of automatically generated inputs, rather than testing specific examples. This approach finds edge cases and bugs that example-based tests often miss.
When to Use
Testing algorithms with mathematical properties
Verifying invariants that should always hold
Finding edge cases automatically
Testing parsers and serializers (round-trip properties)
Validating data transformations
Testing sorting, searching, and data structure operations
Discovering unexpected input combinations
Quick Start
Minimal working example:
# test_string_operations.py
import pytest
from hypothesis import given, strategies as st, assume, example
def reverse_string(s: str) -> str:
"""Reverse a string."""
return s[::-1]
class TestStringOperations:
@given(st.text())
def test_reverse_twice_returns_original(self, s):
"""Property: Reversing twice returns the original string."""
assert reverse_string(reverse_string(s)) == s
@given(st.text())
def test_reverse_length_unchanged(self, s):
"""Property: Reverse doesn't change length."""
assert len(reverse_string(s)) == len(s)
@given(st.text(min_size=1))
def test_reverse_first_becomes_last(self, s):
"""Property: First char becomes last after reverse."""
reversed_s = reverse_string(s)
assert s[0] == reversed_s[-1]
assert s[-1] == reversed_s[0]
// ... (see reference guides for full implementation)
Reference Guides
Detailed implementations in the references/ directory:
Guide
Contents
Hypothesis for Python
Hypothesis for Python
fast-check for JavaScript/TypeScript
fast-check for JavaScript/TypeScript
junit-quickcheck for Java
junit-quickcheck for Java
Best Practices
✅ DO
Focus on general properties, not specific cases
Test mathematical properties (commutativity, associativity)
Verify round-trip encoding/decoding
Use shrinking to find minimal failing cases
Combine with example-based tests for known edge cases
Test invariants that should always hold
Generate realistic input distributions
❌ DON'T
Test properties that are tautologies
Over-constrain input generation
Ignore shrunk test failures
Replace all example tests with properties
Test implementation details
Generate invalid inputs without constraints
Forget to handle edge cases in generators
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: aj-geddes-useful-ai-prompts-property-based-testing3description: Property-Based Testing4---56# Property-Based Testing78## Table of Contents910- [Overview](#overview)11- [When to Use](#when-to-use)12- [Quick Start](#quick-start)13- [Reference Guides](#reference-guides)14- [Best Practices](#best-practices)1516## Overview1718Property-based testing verifies that code satisfies general properties or invariants for a wide range of automatically generated inputs, rather than testing specific examples. This approach finds edge cases and bugs that example-based tests often miss.1920## When to Use2122- Testing algorithms with mathematical properties23- Verifying invariants that should always hold24- Finding edge cases automatically25- Testing parsers and serializers (round-trip properties)26- Validating data transformations27- Testing sorting, searching, and data structure operations28- Discovering unexpected input combinations2930## Quick Start3132Minimal working example:3334```python35# test_string_operations.py36import pytest37from hypothesis import given, strategies as st, assume, example3839def reverse_string(s: str) -> str:40 """Reverse a string."""41 return s[::-1]4243class TestStringOperations:44 @given(st.text())45 def test_reverse_twice_returns_original(self, s):46 """Property: Reversing twice returns the original string."""47 assert reverse_string(reverse_string(s)) == s4849 @given(st.text())50 def test_reverse_length_unchanged(self, s):51 """Property: Reverse doesn't change length."""52 assert len(reverse_string(s)) == len(s)5354 @given(st.text(min_size=1))55 def test_reverse_first_becomes_last(self, s):56 """Property: First char becomes last after reverse."""57 reversed_s = reverse_string(s)58 assert s[0] == reversed_s[-1]59 assert s[-1] == reversed_s[0]60// ... (see reference guides for full implementation)61```6263## Reference Guides6465Detailed implementations in the `references/` directory:6667| Guide | Contents |68|---|---|69| [Hypothesis for Python](references/hypothesis-for-python.md) | Hypothesis for Python |70| [fast-check for JavaScript/TypeScript](references/fast-check-for-javascripttypescript.md) | fast-check for JavaScript/TypeScript |71| [junit-quickcheck for Java](references/junit-quickcheck-for-java.md) | junit-quickcheck for Java |7273## Best Practices7475### ✅ DO7677- Focus on general properties, not specific cases78- Test mathematical properties (commutativity, associativity)79- Verify round-trip encoding/decoding80- Use shrinking to find minimal failing cases81- Combine with example-based tests for known edge cases82- Test invariants that should always hold83- Generate realistic input distributions8485### ❌ DON'T8687- Test properties that are tautologies88- Over-constrain input generation89- Ignore shrunk test failures90- Replace all example tests with properties91- Test implementation details92- Generate invalid inputs without constraints93- Forget to handle edge cases in generators9495---96> Converted and distributed by [TomeVault](https://tomevault.io/claim/aj-geddes) — claim your Tome and manage your conversions.97<!-- tomevault:4.0:skill_md:2026-04-11 -->
Run npx skillmds@latest add tomevault-io/aj-geddes-useful-ai-prompts-property-based-testing in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Property-Based Testing It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
tomevault-io (@tomevault-io) published this skill. Their other Agent Skills are listed on their SkillMD profile.