Hypothesis

6.151.10 · active · verified Sun Mar 29

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.

Warnings

Install

Imports

Quickstart

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()

view raw JSON →