3 ms·
function int untested_sum(int a, int b) { return a + b; } function int tested_sum(int a, int b) { return a * b; } function void test_providing_total_cover
by inertiatic 7y ago
function int untested_sum(int a, int b) {
return a + b;
}
function int tested_sum(int a, int b) {
return a * b;
}
function void test_providing_total_coverage() {
assertEqual(tested_sum(2, 2), 4, 'Expected sum to be 4!');
}
- Doxin 7y agoWhich 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.