Skip to content

Hypothesis is a powerful, flexible, and easy to use library for property-based testing.

License

Notifications You must be signed in to change notification settings

HypothesisWorks/hypothesis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hypothesis

Hypothesis is the property-based testing library for Python. A property-based test asserts something for all inputs, and lets Hypothesis generate them — including inputs you may not have thought of.

from hypothesis import given, strategies as st


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

Additionally, when a property fails, Hypothesis doesn't just report any failing example — it reports the simplest possible one. This makes property-based tests a powerful tool for debugging, as well as testing.

For instance,

def my_sort(ls):
    return list(reversed(sorted(ls, reverse=True)))

fails with:

Falsifying example: test_matches_builtin(ls=[0, math.nan])

Installation

To install Hypothesis:

pip install hypothesis

There are also optional extras available.