/qa-property-based:hypothesis-testingfrom qa-property-based
Generates property-based tests in Python using Hypothesis: @given strategies, composite strategies, assume/filtering, settings, and round-trip/metamorphic property templates. Useful for finding edge cases in parsers, serializers, and numeric code.
Ships in qa-property-based. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
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.
new(x) == old(x) for all x).decode(encode(x)) == x).If only one or two specific examples need verification, parametrize or fixtures suffice - Hypothesis is overkill.
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.
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.
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.
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.
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.
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.
Two of the most useful property patterns:
import json
@given(st.dictionaries(st.text(), st.integers()))
def test_json_round_trip(d):
assert json.loads(json.dumps(d)) == d
If decode(encode(x)) == x for all valid x, the encode/decode
pair is correct.
@given(st.lists(st.integers()))
def test_sort_idempotent(xs):
assert sorted(sorted(xs)) == sorted(xs)
@given(st.lists(st.integers()), st.integers())
def test_sort_commutes_with_offset(xs, offset):
sorted_xs = sorted(xs)
sorted_offset = sorted(x + offset for x in xs)
assert all(s + offset == o for s, o in zip(sorted_xs, sorted_offset))
Metamorphic tests are powerful when the function's "correct output" is hard to specify but its relationship to other inputs is easy.
Per hyp-quickstart: "Hypothesis works seamlessly with pytest fixtures and parametrize decorators."
@pytest.fixture
def db_conn():
# ... setup ...
yield conn
# ... teardown ...
@given(st.integers(0, 1000))
def test_with_fixture(db_conn, n):
db_conn.insert(id=n)
assert db_conn.find(id=n) == n
Hypothesis re-runs the test body with new n each time; the
fixture is set up once per test (per pytest's normal scope rules,
unless it's function-scoped - then once per generated case).
- run: pytest --hypothesis-seed=42 # deterministic seed for reproducibility
# OR via @settings(derandomize=True)
When a property test fails, the failure includes the falsifying example - copy that into a regression test:
@given(...)
@example(n=42) # the falsifying example from the prior run
def test_my_property(n):
...
@example cases run before generated ones; locks the regression in
permanently.
@settings table, anti-patterns, limitations.@given,
strategies, shrinking, composite, assume(), settings.fast-check-testing,
proptest-testing,
jqwik-testing,
quickcheck-testing -
per-language siblings with similar shape.schemathesis-fuzzing - applies PBT to API schemas (different layer; same conceptual
framework).evals/ci-only-failures-nobody-can-reproduce/criteria.json (opens in a new tab)evals/ci-only-failures-nobody-can-reproduce/task.md (opens in a new tab)evals/green-suite-that-missed-the-csv-outage/criteria.json (opens in a new tab)evals/green-suite-that-missed-the-csv-outage/task.md (opens in a new tab)evals/nightly-property-job-went-from-four-minutes-to-forty/criteria.json (opens in a new tab)evals/nightly-property-job-went-from-four-minutes-to-forty/task.md (opens in a new tab)evals/pricing-rewrite-behind-a-flag-before-cutover/criteria.json (opens in a new tab)evals/pricing-rewrite-behind-a-flag-before-cutover/task.md (opens in a new tab)evals/two-red-properties-the-night-before-the-release-cut/criteria.json (opens in a new tab)evals/two-red-properties-the-night-before-the-release-cut/task.md (opens in a new tab)references/hypothesis-reference.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
4 skills in this plugin
/fast-check-testingUse for any question or task about property-based or generated-input tests in JavaScript or TypeScript, including quick how-to questions, round-trip checks like decode(encode(x)), and edge cases example tests keep missing: fast-check with fc.assert and fc.property, arbitraries, shrinking and seeds, in Jest, Vitest or Mocha. Has a worked example./jqwik-testingUse for any question about property-based or generated-input tests in Java or Kotlin, including quick how-to questions, invariants like commutativity or adding zero, and edge cases nobody thought of: jqwik on JUnit 5 with @Property, @ForAll, Arbitraries, @Provide generators, shrinking and statistics./proptest-testingUse for any question or task about property-based or generated-input tests in Rust, including quick how-to questions, round-trip checks and parsers that crash on fuzzed input: proptest with the proptest! macro, strategies for collections, regex strings and any, shrinking, and the persisted regression file.npx claudepluginhub testland/qa --plugin qa-property-basedOther plugins' skills for the same job