explaingit

hypothesisworks/hypothesis

8,619PythonAudience · developerComplexity · 2/5Setup · easy

TLDR

Python testing library that automatically generates hundreds of random inputs to find edge-case bugs your hand-written tests miss, then shrinks any failure to the smallest possible example.

Mindmap

mindmap
  root((hypothesis))
    What it does
      Property-based testing
      Random input generation
      Failure shrinking
    How it works
      Describe input shape
      Auto-generate examples
      Find minimal failure
    vs manual tests
      Finds edge cases
      Less code to write
    Audience
      Python developers
      QA engineers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Test a sorting or parsing function against thousands of auto-generated inputs instead of writing each case by hand.

USE CASE 2

Find duplicate-handling or overflow bugs in custom data processing code by letting Hypothesis probe edge cases automatically.

USE CASE 3

Replace a slow hand-written fuzz test with a concise Hypothesis strategy that checks a property across many input shapes.

USE CASE 4

Add property-based tests to an existing pytest suite with one pip install and one decorator on a test function.

Tech stack

Python

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

Hypothesis is a Python testing library based on an approach called property-based testing. In ordinary unit testing, a developer writes specific input values and checks that the code produces the expected output. With Hypothesis, you describe the shape of the inputs you want to test, such as "a list of integers," and the library automatically generates many random examples from that description and runs your test against all of them. The goal is to check that a property holds across a broad range of inputs rather than just the handful you thought to write by hand. This approach tends to find bugs that example-based tests miss, especially edge cases like empty lists, very large numbers, duplicate values, or unusual combinations that a developer might not think to try. In the README's example, a custom sorting function is tested by asking Hypothesis to generate random lists and verify that the custom function always produces the same result as Python's built-in sort. The test discovers that the custom function incorrectly removes duplicates, and Hypothesis reports the simplest possible failing input: a list containing two identical numbers. When Hypothesis finds a failure, it does not just report the first random example that broke the test. It automatically shrinks the failing input down to the smallest version that still triggers the bug. This makes the output easier to understand and debug compared to staring at a large random test case. Hypothesis is installed with a single pip command. Optional extras add support for additional data types and integrations. The library is primarily for Python, though the repository also contains ports to other languages. Documentation and community links are provided in the README.

Copy-paste prompts

Prompt 1
Show me how to write a Hypothesis test in pytest that checks my custom sort function works correctly on any list of integers.
Prompt 2
I have a URL parser function, write Hypothesis strategies to find edge cases that cause it to crash or return wrong results.
Prompt 3
How do I use Hypothesis to generate realistic user form data for testing a validation function?
Prompt 4
Add Hypothesis property-based tests to a function that merges two sorted lists and verify it handles duplicates correctly.
Prompt 5
My test found a failing case, how does Hypothesis shrinking work and how do I get the minimal failing input from it?
Open on GitHub → Explain another repo

← hypothesisworks on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.