Hypothesis: Property-Based Testing for Python

Hypothesis: Property-Based Testing for Python

Summary

Hypothesis is a powerful property-based testing library for Python, designed to help developers write more robust and reliable code. It automatically generates diverse test inputs, including challenging edge cases, to uncover bugs that traditional testing methods might miss. When a bug is found, Hypothesis simplifies debugging by providing the simplest possible failing example.

Repository Info

Updated on October 22, 2025
View on GitHub

Tags

Click on any tag to explore related repositories

Introduction

Hypothesis is the leading property-based testing library for Python. Instead of writing tests for specific examples, Hypothesis allows you to define properties that your code should satisfy for a whole range of inputs. It then intelligently generates a wide variety of test cases, including common edge cases and unexpected values, to rigorously check these properties. This approach helps you discover subtle bugs and vulnerabilities that might otherwise go unnoticed.

Installation

Getting started with Hypothesis is straightforward. You can install it using pip:

pip install hypothesis

For additional functionalities, Hypothesis also offers optional extras.

Examples

Hypothesis makes it easy to write tests that cover a broad spectrum of inputs. Here's an example demonstrating how to test a sorting function:

from hypothesis import given, strategies as st


@given(st.lists(st.integers()))
def test_matches_builtin(ls):
    assert sorted(ls) == my_sort(ls)

In this example, @given(st.lists(st.integers())) tells Hypothesis to generate lists of integers for the ls argument. Hypothesis will then run test_matches_builtin with many different lists, ensuring my_sort behaves correctly across various scenarios. When a bug is found, Hypothesis provides the simplest possible failing example, like ls=[0, 0] for a faulty my_sort implementation:

def my_sort(ls):
    return sorted(set(ls))

This immediate feedback with minimal examples significantly speeds up the debugging process.

Why Use Hypothesis?

Using Hypothesis brings several key advantages to your testing workflow:

  • Finds Edge Cases Automatically: Hypothesis excels at generating inputs that developers often overlook, such as empty lists, large numbers, or specific combinations of values, leading to more comprehensive test coverage.
  • Simplifies Debugging: When a test fails, Hypothesis provides the smallest possible input that triggers the failure. This minimal example drastically reduces the time and effort required to understand and fix the bug.
  • Improves Code Robustness: By testing against a vast range of inputs, Hypothesis helps ensure your code is resilient and behaves correctly under unexpected conditions, leading to more reliable software.
  • Encourages Property-Based Thinking: It shifts the focus from "what inputs should I test?" to "what properties should my code always maintain?", fostering a deeper understanding of your code's behavior.

Links

Explore Hypothesis further through its official resources: