Property-based testing checks that a rule holds for many automatically generated inputs instead of a handful you picked by hand. In Python, the Hypothesis library does this: you describe the kind of data a function accepts, state a property that must always be true, and Hypothesis generates hundreds of cases, hunts for a failing one and shrinks it to the smallest example. This guide covers hypothesis property based testing with pytest, using Hypothesis 6.x on Python 3.13 or 3.14.
Example-based tests answer "does this input work?". Property-based tests answer "does this rule hold for every input of this shape?", which finds edge cases you would not think to write.
An example test fixes input and output: assert sorted([3, 1, 2]) == [1, 2, 3]. A property describes something true for all valid inputs, for instance "the output of sorted has the same length as the input and each item is less than or equal to the next".
Common property shapes:
| Shape | Example |
|---|---|
| Round trip | decode(encode(x)) == x |
| Invariant | Sorting never changes the length |
| Idempotence | normalize(normalize(x)) == normalize(x) |
| Oracle | A fast version matches a slow, obviously correct one |
| No crash | Parsing any string either returns a result or raises the documented error |
@given runs the test body many times (100 examples by default) with values drawn from the strategy st.lists(st.integers()). pytest collects the function as a normal test, so pytest runs it with no extra setup.
A strategy tells Hypothesis how to build values. The strategies module covers the basics:
You can combine them. st.builds calls a class or function with generated arguments, and st.tuples makes fixed-shape values:
Use .filter(...) sparingly, because rejecting many values slows generation; prefer building valid data directly or use hypothesis.assume for rare conditions. @st.composite lets you write a custom strategy as a function when values depend on each other.
When a property fails, Hypothesis does not show the first random failure. It shrinks the input toward a minimal one. Consider a buggy function:
The failing report looks like this:
An empty list is exactly the edge case a human might forget. After the fix (raise a clear ValueError or require min_size=1) the test passes. Hypothesis also saves failing examples in a local .hypothesis directory and replays them on the next run, so a bug does not vanish by chance.
A round-trip property is one of the most useful patterns:
@example adds a fixed case that always runs, which is the right place for a regression you have already seen. You control the effort with @settings:
deadline fails a single example that runs too slowly (200 ms by default); set deadline=None for tests that call slow code. For team-wide defaults, register a profile in conftest.py with settings.register_profile and select it with --hypothesis-profile.
For objects that change over time, such as a cache or a shopping cart, Hypothesis offers RuleBasedStateMachine. You define rules (operations) and invariants, and Hypothesis generates sequences of operations to find a state that breaks them. Reach for it after you are comfortable with @given.
Good habits:
.hypothesis unless your team wants shared examples; add it to .gitignore.@example for every bug a property found.No. Keep example tests for documented behaviour and readable specifications, and add properties for wide input spaces such as parsers, serializers and maths.
A property that fails only sometimes usually points to a real edge case, or the code under test depends on hidden state such as time. The saved example database replays the failing input so you can reproduce it.
100 by default per test. Raise max_examples in @settings for deeper checks, for example in a nightly CI job.
Yes, but function-scoped fixtures are not reset between generated examples, and Hypothesis warns about this. Prefer building state inside the test or using a function-scoped fixture only for read-only data.
Property-based testing with Hypothesis states a rule once and checks it against many generated inputs. Describe inputs with strategies, write properties such as round trips and invariants, rely on shrinking to get a minimal failing case and use @example to pin regressions. It complements example tests and is strongest on code with a wide input space.