Slide 9
Slide 9 text
from hypothesis import given
from hypothesis.strategies import floats
def sum(x, y):
return x + y
@given(floats(), floats())
def test_sum(x, y):
# sum() is commutative
assert sum(x, y) == sum(y, x)
# 0 is identity
assert sum(x, 0) == x
Example property test
Now try with floats instead of integers
>>> test_sum()
Falsifying example: test_sum(
x=0.0, y=nan,
)
Traceback (most recent call last):
File "", line 1, in
File "", line 5, in test_sum
File "/Users/aaronmeurer/anaconda3/lib/python3.7
1142, in wrapped_test
raise the_error_hypothesis_found
File "", line 7, in test_sum
AssertionError
Hypothesis found an
example that failed the
test