visual studio 2012 - Actual And Expected values are same but unit test fails. Why? -
i trying learn unit test on vs 2012 , tried this:
public class calculator { public double substraction(double num1, double num2) { return num1 - num2; } }
and test class here:
[testclass] public class unittest1 { [testmethod] public void calc_substract() { var calculator = new calculator(); assert.areequal<double>(calculator.substraction(5.3, 1.1), 4.2); } }
gives error:
test name: calc_substract test fullname: myunittest.unittest1.calc_substract test source: d:\users\... test outcome: failed test duration: 0:00:00,0068363 **result message: assert.areequal failed. expected:<4,2>. actual:<4,2>.**
as see, values same test fails. know why?
very odd, i'm wondering if it's loss of precision issue since you're working doubles - although values seem innocuous enough not trigger precision issues.
also, fantastic result message :).
does @ least pass if ask compare 4.2 4.2:
assert.areequal<double>(4.2, 4.2);
?
would curious see happens if plug in rounding:
assert.areequal<double>(math.round(calculator.substraction(5.3, 1.1),1), 4.2);
also, consider using decimal
instead of double
has greater precision , more naturally represents exact decimals, should find precision issues cases specific test avoided decimal
- although that's not other, more complex cases, won't still suffer precision issues decimal
Comments
Post a Comment