The property-based testing library for Python
Hypothesis is the property-based testing library for Python. With Hypothesis, you write tests which should pass for all inputs in whatever range you describe, and let Hypothesis randomly choose which of those inputs to check - including edge cases you might not have thought about. For example:
from hypothesis import given, strategies as st
@given(st.lists(st.integers()))
def test_matches_builtin(ls):
assert sorted(ls) == my_sort(ls)This randomized testing can catch bugs and edge cases that you didn't think of and wouldn't have found. In addition, when Hypothesis does find a bug, it doesn't just report any failing test case — it reports the simplest possible one. This makes property-based tests a powerful tool for debugging, as well as testing.
For instance,
def my_sort(ls):
return sorted(set(ls))fails with the simplest possible failing test case:
Failing test case: test_matches_builtin(ls=[0, 0])To install Hypothesis:
pip install hypothesisThere are also optional extras available.
Unintended error message raised when using `data().map(pack=str)`
test_assume_has_status_reason fails under the crosshair backend because of a CPython sys.monitoring bug
Ghostwriter writes @given() for functions with no parameters
Ghostwriter omits imports for filters built from annotated_types bounds
Migrate the Hypothesis engine from Python to Rust
Skill to migrate from pytest
Improve django timezone library for modern standards
Ideas for more blog posts
Documentation overhaul
Develop a strategy for symbolic backend support alongside rust-based hypothesis logic