Registry / testing / hypothesis

hypothesis

JSON →
library6.151.10pypypi✓ verified 52d ago

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. This randomized testing can catch bugs and edge cases that you didn't think of and wouldn't have found. When Hypothesis finds a bug, it reports the simplest possible example. It is currently at version 6.151.10 and receives regular updates.

testing
pip install hypothesis
Install & Compatibility
Where this runs
tested against v6.155.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.776s · 21.9MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 2.3s · import 0.685s · 22MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

given
from hypothesis import given
The primary decorator for property-based tests.
strategies
from hypothesis import strategies as st
Commonly imported as 'st' for convenience to access built-in strategies.

This quickstart demonstrates a basic property-based test using Hypothesis. The `@given` decorator takes a strategy (here, `st.integers()`) and repeatedly calls the decorated test function with generated inputs, looking for counterexamples.

from hypothesis import given, strategies as st def my_function(x): return x + 1 @given(st.integers()) def test_my_function_increments(n): assert my_function(n) == n + 1 # To run this, save as a Python file (e.g., example.py) and run with pytest # For manual execution, uncomment and call the function: # test_my_function_increments()
hypothesis --version
Debug
Known issues
breakingDocumented APIs only guarantee backward compatibility across major version bumps. Undocumented attributes, modules, and behavior may include breaking changes in patch or minor releases. Always check the changelog for significant updates.
fix
Refer to the official documentation and changelog before upgrading major versions or relying on undocumented features. Pin minor versions for stability in production.
affects: All versions
deprecatedDeprecated features will emit a `HypothesisDeprecationWarning` for at least six months before being removed in a subsequent major release.
fix
Pay attention to `HypothesisDeprecationWarning` messages and update your code to use the recommended alternatives to avoid breakage in future major releases.
affects: All versions
gotchaTests must have deterministic outcomes and data generation. If tests involve threads or other non-deterministic elements and data generation depends on timing, Hypothesis may report flaky failures.
fix
Refactor data generation to be independent of test timings or other non-deterministic factors. Ensure that a given input always produces the same outcome during the test execution.
affects: All versions
gotchaUsing overly broad strategies (e.g., `st.text()` without length limits) can lead to extremely slow test runs, high memory consumption, or tests that struggle to find simple counterexamples due to the vast search space.
fix
Constrain strategies as much as possible to the domain relevant to your code. For instance, use `st.text(min_size=1, max_size=255, alphabet=string.ascii_letters)` instead of a generic `st.text()`.
affects: All versions
gotchaThe `.example()` method on strategies is intended for interactive use (e.g., in a REPL) and should not be used within test functions or production code.
fix
Always use the `@given` decorator for defining property-based tests. `.example()` is for exploring what a strategy generates, not for testing assertions.
affects: All versions
Errors
Common errors & fixes
hypothesis.errors.InvalidArgument: Expected SearchStrategy but got function
This error occurs when a strategy function (e.g., `st.integers`) is passed to `@given` or another strategy combinator without being called, meaning the function itself is passed instead of the strategy it returns.
fix
Ensure that all strategy functions are called with parentheses, even if they take no arguments (e.g., use `st.integers()` instead of `st.integers`).
hypothesis.errors.UnsatisfiableExample
Hypothesis raises this error when it cannot find enough examples that satisfy the constraints imposed by `assume()` calls, `.filter()` methods, or an overly restrictive strategy definition, indicating that the conditions might be too hard or impossible to meet.
fix
Review your `assume()` and `.filter()` conditions to ensure they do not eliminate too many valid examples. Consider if your strategy definition allows for a sufficient range of inputs to satisfy your test's assumptions.
hypothesis.errors.ResolutionFailed: Could not resolve ... to a strategy
This error occurs when Hypothesis attempts to automatically infer a strategy for a type, often in `builds()`, but fails to find a suitable mapping, particularly with custom types or when type annotations are missing or unresolvable.
fix
Explicitly provide a strategy for the type in question using `st.builds()` or ensure that the type hints are correct and Hypothesis has a registered strategy for that type.
Upgrade
Version history
6.155.2latest on PyPI
Audit
Dependencies

No dependency data recorded yet.

Agent activity
51 hits · last 30 days
seranking-bot
4
ahrefsbot
3
node
2
Amazon
1
bingbot
1
Resources