I'm just beginning to use hypothesis for test generation. How can I allow certain errors in the test function when I use hypothesis?
Simple example is a division function which works for integers > 0:
def divide(a: int, b: int) -> float:
return a/b
When I write a hypothesis test without explicitly excluding 0 I get errors of course:
from hypothesis import given, strategies as st
@given(a=st.integers(), b=st.integers())
def test_divide(a, b):
assert isinstance(divide(a,b), float)
Since hypothesis uses the integer 0 I get ZeroDivisionError
:
---------------------------
Falsifying example: test_divide(
a=0, b=0,
)
============================
FAILED test.py::test_divide - ZeroDivisionError: division by zero
This is to be expected of course and in this simple example I could simply exclude 0 because I know the error will happen. In other functions, especially the ones working with strings, it would be difficult to exclude all things where I know that an error is thrown. In many cases though the error is ok, e.g. when using null bytes in string functions.
Is it possible to somehow 'allow' certain types of errors?
Or do I have to rethink how to write tests when using hypothesis?