What this thread does
Install Hypothesis in your test environment, write a meaningful invariant with @given, and run it through pytest alongside your existing examples.
Setup recipe
Do first: run python -m pip install pytest hypothesis==6.170.17 in your test environment. Pick one pure function with a round-trip, invariant or independent reference implementation. State the valid input domain before generating values. The Hypothesis quickstart explains strategies and ordinary pytest collection; checked 2026-11-20.
Use Python 3.11 or later in the project's virtual environment:
python -m pip install pytest hypothesis==6.170.17
Record resolved dependencies in your project's existing lock mechanism. Save this authored starting example as test_properties.py:
import json
from hypothesis import example, given, strategies as st
@given(st.lists(st.integers(), max_size=30))
@example([0, 0])
def test_json_integer_list_round_trip(values):
encoded = json.dumps(values)
assert json.loads(encoded) == values
Then run:
python -m pytest -q test_properties.py
A passing run confirms integration, not coverage of your product. Replace the JSON pair with your own encoder/decoder, or compare your implementation to an independent reference. Keep the property stronger than “does not crash.”
Add a deliberately broken implementation in a disposable test copy, such as removing duplicates before encoding. The duplicate example should fail. Restore the implementation; do not weaken the assertion. This is a proposed acceptance check, not a claimed result.
Replay and test budget
The replay guide documents @example and @reproduce_failure. With a configured example database, failures are saved and replayed automatically. The built-in ci profile disables the database. Keep durable regressions as readable @example inputs; reproduction blobs are not a long-term portable test format.
The settings API lists max_examples=100 in the default profile; this is a stopping budget, not proof of exhaustive input coverage. It also documents a CI profile. Select and record your intended profile rather than assuming every environment uses identical settings. Checked 2026-11-20.
Which test style wins?
| Option | Wins | Loses | Pick this if |
|---|---|---|---|
| pytest examples / parametrization | Named business rules and known regressions | Only exercises selected values | Expected outputs need explicit review |
| Hypothesis with pytest | Explores a stated input domain and simplifies failures | A weak property can pass while code is wrong | You can state an invariant independent of the implementation |
The default for solo developers and small teams is both: keep examples and add one property around a bug-prone boundary. Enterprise default: introduce a bounded property suite with reviewed generators and failure replay, then expand based on useful findings.
Version and operational cost, checked 2026-11-20
As of November 2026, PyPI and the current official documentation identify Hypothesis 6.170.17. This recipe uses the regular local test runner, without a hosted service. Generated cases consume test runtime; the example's list-size cap is an authored starting budget, not a library limit. No paid-service price is asserted.
Public evidence
ckp95 opened Hypothesis issue 2731 on 2021-01-02: “the fixture is reused across” generated examples. The report concerns a stateful spreadsheet fixture; it illustrates why fixture lifetime must be reviewed. It is historical evidence, not a current unresolved defect.
Agent prompt and mistakes
Keep our existing examples. Add one property with a reviewed input domain, show a deliberately broken variant failing, then restore it. Preserve a useful counterexample. Explain any mutable fixture state shared across generated cases.
Do not hide health checks to avoid understanding state leakage. Reset per-case state inside the test when independence is required; a pytest fixture is not automatically recreated for every generated input. Avoid filtering almost every generated value; construct valid values directly where possible.
Limits
No installation or test run was performed. The JSON example tests integration; it must be adapted to the reader's code. It does not cover state machines, async test wrappers or external-system cleanup. Public evidence is narrow and historical.
Started from a post by @Hypothesis team, packaged by the editor. Original post