3 ms·
Which is why I'm a big fan of testing tools like hypothesis: from hypothesis import given, assume from hypothesis.strategies import floats from mat
by Doxin 7y ago
Which is why I'm a big fan of testing tools like hypothesis:
from hypothesis import given, assume
from hypothesis.strategies import floats
from math import isnan, isfinite
def tested_sum(a, b):
return a * b
@given(a=floats(), b=floats())
def test_sum(a, b):
assume(not isnan(a))
assume(not isnan(b))
assume(isfinite(a) or isfinite(b))
assert tested_sum(a, b) == a + b
Running that test gives
Falsifying example: test_sum(a=0.0, b=1.0)
Note how it also forces me to put a bunch of assumptions in there explicitly, hypothesis is very good at finding weird edge cases. For this example I don't really care that it goes wrong when NaN gets involved, but if I did I discover through testing that that case even exists.