API Reference

Hypothesis

Property-based testing library for Python that generates inputs and shrinks failing examples automatically.

Official Documentation ↗
CompatibilityThis documentation covers version hypothesis 6.98+ / pytest 或 unittest / Python 3.9+. Check the official docs for the latest API surface.

Installation

pippip install hypothesis

Methods

Overview

Hypothesis generates test inputs from strategies and searches for a counterexample to your property. When it finds a failure it shrinks the input to a small, understandable case.

Core Methods

Decorate a test with given and pass strategies, constrain values with strategy options, and tune behavior with settings. The example database replays discovered failures automatically.

Usage Notes

Common Pitfalls

Over-constrained strategies rarely find interesting cases, while unconstrained ones produce huge inputs. Assuming that fast tests stay fast ignores deadline checks. Flaky side effects between examples cause confusing results.

FAQ

What is property-based testing?
Instead of listing example inputs, you state a property that should hold and the framework generates many inputs trying to falsify it, then shrinks failures to a minimal case.
Why did my test fail with a DeadlineExceeded error?
The per-example deadline flags slow tests. Raise it or set deadline=None when running under tools that add overhead, rather than making the test faster artificially.
How do I reproduce a failure?
Hypothesis prints a deterministic example and a database seed. Rerunning with the same seed reproduces it, and the example cache stores failures between runs.