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):
/tmp/ipykernel_8817/1742712940.py:2:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
ls = [9.9792015476736e+291, 1.7976931348623157e+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.7976931348623157e+308
E + where 1.7976931348623157e+308 = max([9.9792015476736e+291, 1.7976931348623157e+308])
E Falsifying example: test_mean(
E ls=[9.9792015476736e+291, 1.7976931348623157e+308],
E )
/tmp/ipykernel_8817/1742712940.py:4: AssertionError
========================================= warnings summary =========================================
../../../../../../.local/share/virtualenvs/python-311-6zxVKbDJ/lib/python3.11/site-packages/_pytest/config/__init__.py:1204
/Users/veit/.local/share/virtualenvs/python-311-6zxVKbDJ/lib/python3.11/site-packages/_pytest/config/__init__.py:1204: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: hypothesis
self._mark_plugins_for_rewrite(hook)
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
===================================== short test summary info ======================================
FAILED t_97777e739d3141398e41c86d782e924f.py::test_mean - assert inf <= 1.7976931348623157e+308
1 failed, 1 warning in 0.63s
Installation#
$ pipenv install hypothesis
Alternatively, Hypothesis can also be installed with extras, for example
$ 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.
See also: