Testing & Quality Assurance

hypothesis

Instead of you thinking up test cases, this library invents thousands of weird ones trying to break your code.

Install it: pip install hypothesis

What does it do?

Normal tests check specific examples, like does this function return 4 when given 2 and 2. Hypothesis flips that around: you describe the general shape of valid input, such as any two whole numbers, and it automatically generates hundreds of random, often bizarre combinations, including edge cases a human would never think to try, like zero, negative numbers, or absurdly huge values, to see if your code breaks. It’s called property-based testing because you’re testing that a general property always holds, not just a handful of examples you happened to pick. When it finds a failure, it automatically works backward to find the simplest possible input that still breaks things.

See it in action

This checks a basic math rule — that addition works the same regardless of order — by automatically trying it out on hundreds of randomly generated number pairs.

from hypothesis import given
from hypothesis import strategies as st

@given(st.integers(), st.integers())
def test_addition_is_commutative(a, b):
    assert a + b == b + a

Why would a non-developer care?

Most software bugs hide in the edge cases nobody thought to test, like the empty list, the negative number, the string with an unexpected character. Hypothesis exists specifically to find those blind spots automatically, catching mistakes before they ever reach a real user.

Real-world examples

Hypothesis is inspired by a similar tool called QuickCheck, originally built for the Haskell programming language, and it’s used by projects that need very high reliability, including parts of the CPython interpreter’s own test suite. A team relying only on hand-written example tests can pass every test they wrote and still ship a bug the moment a user does something the developer never imagined, exactly the scenario Hypothesis catches in advance.

Who uses it

Developers writing tests for code with complex logic or many possible inputs, especially where correctness really matters, like parsers, data validation, or financial calculations.

How it compares to alternatives

It’s a fundamentally different approach from example-based testing tools like pytest or unittest, though it plugs directly into pytest rather than replacing it, so most teams use both together.

Fun fact

The concept Hypothesis is built on, property-based testing, originated with a tool called QuickCheck for the Haskell language in the late 1990s, and Hypothesis brought that idea to Python decades later.

New to Python and want to actually try libraries like this yourself?

Find a beginner-friendly course

Related libraries