hypothesis-testing
FreeNo executable scriptsNot checkedUse for any question or task about property-based or generated-input tests in Python, including quick how-to questions, round-trip checks like…
by testland★ 18 GitHub starsAbout this skill
hypothesis-testing
Overview
Hypothesis is the canonical Python property-based testing library
(hyp-quickstart). It generates inputs from strategies and
automatically shrinks any failure to the simplest reproducer.
When to use
- A Python function has a non-trivial input domain (numeric ranges, string formats, list shapes) and example-based tests miss edge cases.
- A bug fix needs a property test to prevent regression of a class of inputs (not just the one that caused the bug).
- Refactoring code where the new implementation should be
observably equivalent to the old (use Hypothesis to verify
property:
new(x) == old(x) for all x). - Testing parsers / serializers / encoders where round-trip
properties hold (
decode(encode(x)) == x).
If only one or two specific examples need verification, parametrize or fixtures suffice - Hypothesis is overkill.
Step 1 - Install
pip install hypothesis
Pin a version in requirements-dev.txt / pyproject.toml.
Hypothesis is well-maintained but adds 1-2 seconds to test runtime
per generated case; default 100 examples per @given.
Step 2 - Basic property test
Per hyp-quickstart:
from hypothesis import given, strategies as st
@given(st.integers(0, 100))
def test_example(n):
assert n < 50
The decorator runs the test 100 times with random n in [0, 100].
On failure, Hypothesis reports the falsifying example (the
smallest n that violates the assertion) - typically 50 here.
Step 3 - Strategies
Per hyp-quickstart, a strategy names the shape of the input. The common ones:
st.integers(0, 100) # bounded ints
st.floats(allow_nan=False) # floats, optionally excluding NaN/Inf
st.text(min_size=1) # strings
st.lists(st.integers(), max_size=10) # collections
st.from_regex(r"[a-z]+@[a-z]+\.com", fullmatch=True) # format-validated
st.sampled_from(Status) # enum-like
Full catalog (binary, dictionaries, tuples, builds, composite):
references/hypothesis-reference.md.
Step 4 - Composite strategies (dependent fields)
Per hyp-quickstart, @st.composite lets later values depend
on earlier ones via draw():
from hypothesis import strategies as st
@st.composite
def valid_dates(draw):
year = draw(st.integers(1900, 2100))
month = draw(st.integers(1, 12))
if month in (1, 3, 5, 7, 8, 10, 12):
day = draw(st.integers(1, 31))
elif month in (4, 6, 9, 11):
day = draw(st.integers(1, 30))
else:
# Feb: account for leap years
max_day = 29 if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0 else 28
day = draw(st.integers(1, max_day))
return date(year, month, day)
@given(valid_dates())
def test_date_round_trip(d):
assert date.fromisoformat(d.isoformat()) == d
The draw() call requests a value from a strategy; the composite
returns the constructed value.
Step 5 - Filtering and assumptions
Two ways to constrain inputs:
# Filter at strategy level (preferred - Hypothesis can sample efficiently)
@given(st.integers(0, 100).filter(lambda x: x % 2 == 0))
def test_even_squares(n):
assert (n * n) % 2 == 0
# Filter at test level (fallback when filtering depends on multi-input)
from hypothesis import assume
@given(st.integers(), st.integers())
def test_division(a, b):
assume(b != 0) # discard cases where b == 0
# Python's floor-division identity: x == (x//y)*y + (x%y)
assert (a // b) * b + a % b == a
The assertion is Python's documented floor-division identity
(py-expr): a property of // and %, not an expression
that is true for any result.
Per hyp-quickstart, use .filter() at the strategy level
when possible (Hypothesis can sample efficiently). Use assume()
inside the test when the precondition involves multiple inputs.
Heavy filtering is a smell - if 90% of generated cases are discarded, redesign the strategy.
Step 6 - Settings and reproducibility
from hypothesis import given, settings, strategies as st
@settings(max_examples=500, deadline=2000) # 500 cases, 2s deadline per case
@given(st.integers())
def test_expensive(n):
expensive_function(n)
For CI, set derandomize=True to make failures reproducible across
runs (vs random seed = same property test passes locally, fails on
CI mysteriously). Full settings table (max_examples, deadline,
phases, verbosity):
references/hypothesis-reference.md.
Step 7 - Round-trip and metamorphic properties
Two of the most useful property patterns:
Round-trip
import json
@given(st.dictionaries(st.text(), st.integers()))
def test_json_round
Install hypothesis-testing in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
Bundled files
FAQ
What does the hypothesis-testing skill do?
Use for any question or task about property-based or generated-input tests in Python, including quick how-to questions, round-trip checks like parse(format(x)), and inputs that break code in production: Hypothesis @given strategies, composite strategies, assume and filtering, settings, reproducibility and CI. Has round-trip and metamorphic property templates.
How do I install the hypothesis-testing skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the hypothesis-testing skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
by Anthropicclaude-api
Reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
by Anthropicalgorithmic-art
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
by Anthropicmcp-integration
This skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…
by Anthropic