Property-Based Testing

Property-Based Testing

Teaches an AI coding agent to design property-based tests that check invariants over generated inputs, with Hypothesis, fast-check and QuickCheck examples.

Skill files6 files

references/

scripts/

templates/

SKILL.md97 lines
1
---
2
name: property-based-testing
3
description: >
4
Design property-based tests that verify code properties hold for all inputs
5
using automatic test case generation. Use for property-based, QuickCheck,
6
hypothesis testing, generative testing, and invariant verification.
7
---
8
9
# Property-Based Testing
10
11
## Table of Contents
12
13
- [Overview](#overview)
14
- [When to Use](#when-to-use)
15
- [Quick Start](#quick-start)
16
- [Reference Guides](#reference-guides)
17
- [Best Practices](#best-practices)
18
19
## Overview
20
21
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.
22
23
## When to Use
24
25
- Testing algorithms with mathematical properties
26
- Verifying invariants that should always hold
27
- Finding edge cases automatically
28
- Testing parsers and serializers (round-trip properties)
29
- Validating data transformations
30
- Testing sorting, searching, and data structure operations
31
- Discovering unexpected input combinations
32
33
## Quick Start
34
35
Minimal working example:
36
37
```python
38
# test_string_operations.py
39
import pytest
40
from hypothesis import given, strategies as st, assume, example
41
42
def reverse_string(s: str) -> str:
43
"""Reverse a string."""
44
return s[::-1]
45
46
class TestStringOperations:
47
@given(st.text())
48
def test_reverse_twice_returns_original(self, s):
49
"""Property: Reversing twice returns the original string."""
50
assert reverse_string(reverse_string(s)) == s
51
52
@given(st.text())
53
def test_reverse_length_unchanged(self, s):
54
"""Property: Reverse doesn't change length."""
55
assert len(reverse_string(s)) == len(s)
56
57
@given(st.text(min_size=1))
58
def test_reverse_first_becomes_last(self, s):
59
"""Property: First char becomes last after reverse."""
60
reversed_s = reverse_string(s)
61
assert s[0] == reversed_s[-1]
62
assert s[-1] == reversed_s[0]
63
// ... (see reference guides for full implementation)
64
```
65
66
## Reference Guides
67
68
Detailed implementations in the `references/` directory:
69
70
| Guide | Contents |
71
|---|---|
72
| [Hypothesis for Python](references/hypothesis-for-python.md) | Hypothesis for Python |
73
| [fast-check for JavaScript/TypeScript](references/fast-check-for-javascripttypescript.md) | fast-check for JavaScript/TypeScript |
74
| [junit-quickcheck for Java](references/junit-quickcheck-for-java.md) | junit-quickcheck for Java |
75
76
## Best Practices
77
78
### ✅ DO
79
80
- Focus on general properties, not specific cases
81
- Test mathematical properties (commutativity, associativity)
82
- Verify round-trip encoding/decoding
83
- Use shrinking to find minimal failing cases
84
- Combine with example-based tests for known edge cases
85
- Test invariants that should always hold
86
- Generate realistic input distributions
87
88
### ❌ DON'T
89
90
- Test properties that are tautologies
91
- Over-constrain input generation
92
- Ignore shrunk test failures
93
- Replace all example tests with properties
94
- Test implementation details
95
- Generate invalid inputs without constraints
96
- Forget to handle edge cases in generators
97

© AJ Geddes · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…