Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Unit Test: why test run inconclusive whereas testing same float values?

I'm learning VS Unit test and tried this:

    [TestMethod()]
    public void calcTest()
    {
        double expected = 1.234F; // TODO: Initialize to an appropriate value
        double actual;
        actual = 1.234F;
        Assert.AreEqual(expected, actual);
        Assert.Inconclusive("Verify the correctness of this test method.");
    }

When running this test method, it says inconclusive ??? Why ?

Update: Ok to tell don't compare floats, but business requirements are what they are. So what should I do if I need to compare them?

Do you mean it's impossible to test floating calculation without headache? Then if testing is such a headache in financial calculation isn't it better to not do testing at all?

Seems like a huge bug or design flaw in vs test framework rather :) as it is said here http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.inconclusive%28VS.80%29.aspx

Indicates that an assertion cannot be proven true or false.

Since I compare 2 same litterals sure it is true.

like image 815
user310291 Avatar asked Sep 23 '10 11:09

user310291


2 Answers

erm, because you told it to be?

Assert.Inconclusive("Verify the correctness of this test method.");

Now you have your AreEqual, you should be able to remove this Inconclusive

Any failure during a test (not including exceptions that you intentionally handle) is generally terminal, but any assert that passes (like the AreEqual here) just keeps on running. So the first test passes, then the last line flags it as inconclusive.

like image 194
Marc Gravell Avatar answered Sep 27 '22 16:09

Marc Gravell


Even when you've removed the Assert.Inconclusive you still might have problems.

You're testing the equality of two floating point numbers and in general with calculated values you'll never get them exactly the same. You need to check that the actual value is within an acceptable range of the expected value:

Math.Abs(actual - expected) < 0.00001;

for example.

Your Assert.AreEqual(expected, actual); works in this case because you are assigning the same value to both variables.

like image 32
ChrisF Avatar answered Sep 27 '22 15:09

ChrisF