Introduction
Traditional tests check if a function works with specific inputs. But what if you don't know in advance which inputs will cause a failure? Hypothesis – generative testing solves this problem automatically.
Hypothesis is a powerful property-based testing library for Python that fundamentally changes how you approach testing. Instead of manually creating test data, it automatically generates thousands of different input values, actively searching for edge cases, bugs, and unexpected behavior. Rather than writing many tests with specific values, you describe the properties and invariants of your function, and Hypothesis tries to break them.
What is Hypothesis
Hypothesis is a generative testing tool built on the concept of property-based testing. This library was created to solve a fundamental problem with traditional testing: the impossibility of anticipating every possible input.
Core Principles
The library works on the principle of "find a counterexample." When you define a property of a function (for example, that adding two numbers always produces a result greater than each addend), Hypothesis generates a wide variety of inputs and checks whether the property holds for each one.
Advantages Over Traditional Testing
- Automatic edge case coverage: Hypothesis independently finds rare and complex data combinations
- Error minimization: When a test fails, it automatically simplifies the input to the smallest reproducible example
- High performance: Generates thousands of test cases quickly
- Reproducibility: Found bugs can be easily reproduced and debugged
Installation and First Steps
Installing the Library
pip install hypothesis
For additional features, install with extras:
pip install hypothesis[numpy,pandas,django]
Basic Usage Example
from hypothesis import given
import hypothesis.strategies as st
@given(st.integers(), st.integers())
def test_addition_commutative(a, b):
assert a + b == b + a
@given(st.integers(), st.integers())
def test_addition_associative(a, b, c):
assert (a + b) + c == a + (b + c)
Running Tests
Tests can be run with pytest, unittest, or directly:
pytest test_file.py
python -m unittest test_file.py
python test_file.py
Data Generation Basics
The @given Decorator
The @given() decorator is the core of Hypothesis. It specifies which strategies to use for generating test data.
@given(st.integers())
def test_absolute_value(x):
assert abs(x) >= 0
@given(st.text(), st.integers(min_value=0))
def test_string_multiplication(s, n):
result = s * n
assert len(result) == len(s) * n
The hypothesis.strategies Module
All data generation strategies live in the hypothesis.strategies module, typically imported as st. This module provides a rich set of predefined strategies for various data types.
Data Generation Strategies
Basic Strategies
Hypothesis offers an extensive set of built-in strategies for generating different data types:
# Numeric types
st.integers() # Integers
st.floats() # Floating-point numbers
st.decimals() # Decimal numbers
# Text data
st.text() # Unicode strings
st.binary() # Byte strings
st.characters() # Individual characters
# Bool