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.
pip install hypothesisVerified import paths — ran on the pinned version, not inferred.
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.
Refer to the official documentation and changelog before upgrading major versions or relying on undocumented features. Pin minor versions for stability in production.
Pay attention to `HypothesisDeprecationWarning` messages and update your code to use the recommended alternatives to avoid breakage in future major releases.
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.
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()`.
Always use the `@given` decorator for defining property-based tests. `.example()` is for exploring what a strategy generates, not for testing assertions.
Ensure that all strategy functions are called with parentheses, even if they take no arguments (e.g., use `st.integers()` instead of `st.integers`).
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.
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.
No dependency data recorded yet.