Property-based testing · generate inputs, find edge cases · verified against Hypothesis 6.x (2026)

Hypothesis cheat sheet

Hypothesis is property-based testing for Python: instead of hand-picking example inputs, you state a property that should hold for all inputs in a described space, and Hypothesis generates many cases — including nasty edge cases you'd never think of. When it finds a failure it shrinks it to the smallest counterexample and saves it so the bug reproduces deterministically. It runs on top of pytest/unittest. This sheet targets Hypothesis 6.x.

@given & the idea strategies composing strategies control & settings stateful & extras gotcha most common

Verified 2026-08-24 against the official docs at hypothesis.readthedocs.io and the HypothesisWorks/hypothesis repo (6.x). Integrates with pytest & unittest; extras for NumPy, pandas & Django.

Outline

The shift: test properties ("sorting is idempotent", "decode(encode(x)) == x"), not specific examples. Describe the input space with strategies; Hypothesis does the rest.

Basics

  1. 1 · Install & @given
  2. 2 · Thinking in properties

Data

  1. 3 · Built-in strategies
  2. 4 · Composing strategies

Control

  1. 5 · example, assume, note
  2. 6 · Settings & targeting

Advanced

  1. 7 · Stateful testing
  2. 8 · NumPy / pandas extras

Reference

  1. 9 · Gotchas
  2. Worth memorizing

Basics

Decorate a test with the inputs it should hold for.

1Install & @given6.x
2Thinking in propertieswhat to assert

Strategies

Describe the space of inputs to draw from.

3Built-in strategiesst.*
4Composing strategiesbuild complex data

Control

Steer generation and tune the run.

5example, assume, notesteer cases
6Settings & targetingtune the run

Advanced

Test sequences of operations, and generate arrays.

7Stateful testingsequences of actions
8NumPy / pandas extrasarray data
!Common gotchasread before shipping

Worth memorizing

test properties, not examplesround-trip, invariant, oracle, metamorphic
@given(st.strategy) maps to paramsruns the body against ~100 generated inputs
shrinking finds the minimal counterexampleand saves it for deterministic replay
constrain floatsallow_nan=False, allow_infinity=False or == breaks
st.builds / @st.compositegenerate your domain objects
@example pins tricky cases0, empty, boundaries always checked
assume() to reject, not filter heavilyover-filtering is slow / unsatisfiable
deadline=None for slow testsavoids false flaky flags
RuleBasedStateMachine for statefulrandom sequences of @rule ops + @invariant