views:

79

answers:

3

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: Hi Guys 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 !

+2  A: 

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.

Marc Gravell
Why Assert.Conclusive doesn't exist ? We're not doing statistical testing where such Inconclusiveness would be sensical, this is deterministic testing where testing result will be yes or no.
I think you're right but the template should not put this as default in code snippet because this is very confusing.
+2  A: 

Doesn't that just mean that the AreEqual passed, which meant it called Assert.Inconclusive, leading to a result of inconclusive?

From the docs:

Similar to Fail in that it indicates an assertion is inconclusive without checking any conditions.

If you don't want the result to be inclusive, remove the call to Assert.Inconclusive :)

Jon Skeet
My point is mathematically and from business viewpoint it's sure not inconclusive but conclusive since it is true :)
Assert.Inconclusive Method Indicates that an assertion cannot be proven true or false.
according to http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.inconclusive%28VS.80%29.aspx
+1  A: 

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.

ChrisF
Good warning, but in this case he's comparing two variables initialized from literals, so I would *expect* it to be OK. But +1, definitely a very good point to raise!
Marc Gravell
@Marc - I spotted that so added a note to that effect.
ChrisF
The fact that accurate floating point representations may not exist for certain decimal numbers does not make them 'fuzzy'. The general point about not comparing 2 floats for equality is fine, but it does not apply in this case: there is no reason that the compiler will choose different `double` representations for the *same* `float` literal.
Ani
Not in this (very artificial case) but in general: Yes, don't compare floats.
Henk Holterman
What don't compare floats but Business Requirements need to compare float :)
The problem with testing < 0.00001; is that in the application which covers many countries and brands the precision will vary so It's rather circumvolated because I'll have to create an algo just for calculating the threshold for test that sucks :)
@user310291 - you *can* compare floats - you just need to be careful due to rounding errors and the fact that not all floats can be represented. As long as you allow for this rounding error you should be OK. Also using `decimal` will reduce the rounding error. My use of `0.00001` was merely meant as an example.
ChrisF
What's the exact syntax I can't see any Assert.IsLessThan ? Thanks.
@user310291 You'll have to use the `Assert.IsTrue` method, passing the test as the argument. See http://msdn.microsoft.com/en-us/library/ms243771(v=VS.80).aspx
ChrisF