Hypothesis#

Hypothesis is a library that allows you to write tests that are parameterised from a source of examples. Then simple and comprehensible examples are generated, which can be used to fail your tests and to find errors with little effort.

Example#

To test lists with floating point numbers, many examples are tried, but only a simple example is given in the report for each bug (unique exception type and position):

[1]:
from hypothesis import given
from hypothesis.strategies import lists, floats
[2]:
# Add ipython magics
import ipytest
import pytest

ipytest.autoconfig()
[3]:
%%ipytest

@given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))
def test_mean(ls):
    mean = sum(ls) / len(ls)
    assert min(ls) <= mean <= max(ls)
F                                                                                            [100%]
============================================= FAILURES =============================================
____________________________________________ test_mean _____________________________________________

    @given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))
>   def test_mean(ls):

/var/folders/f8/0034db6d78s5r6m34fxhpk7m0000gp/T/ipykernel_12395/1742712940.py:2:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

ls = [1.0705975999293683e+307, 1.7976931013776717e+308]

    @given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))
    def test_mean(ls):
        mean = sum(ls) / len(ls)
>       assert min(ls) <= mean <= max(ls)
E       assert inf <= 1.7976931013776717e+308
E        +  where 1.7976931013776717e+308 = max([1.0705975999293683e+307, 1.7976931013776717e+308])

/var/folders/f8/0034db6d78s5r6m34fxhpk7m0000gp/T/ipykernel_12395/1742712940.py:4: AssertionError
-------------------------------------------- Hypothesis --------------------------------------------
Falsifying example: test_mean(
    ls=[1.0705975999293683e+307, 1.7976931013776717e+308],
)
========================================= warnings summary =========================================
../../../.local/share/virtualenvs/jupyter-tutorial-G-MBNaSt/lib/python3.7/site-packages/_pytest/config/__init__.py:1114
  /Users/veit/.local/share/virtualenvs/jupyter-tutorial-G-MBNaSt/lib/python3.7/site-packages/_pytest/config/__init__.py:1114: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: hypothesis
    self._mark_plugins_for_rewrite(hook)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
===================================== short test summary info ======================================
FAILED tmpu8nbuxfq.py::test_mean - assert inf <= 1.7976931013776717e+308
1 failed, 1 warning in 11.25s

Installation#

$ pipenv install hypothesis

Alternatively, Hypothesis can also be installed with extras, e.g.

$ pipenv install hypothesis[numpy,pandas]

Note:

If you haven’t installed pipenv yet, you can find instructions on how to do this in Install pipenv.