visual-studio Visual Studio 单元测试:为什么测试运行没有定论,而测试相同的浮点值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3777970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio Unit Test: why test run inconclusive whereas testing same float values?
提问by user310291
I'm learning VS Unit test and tried this:
我正在学习 VS 单元测试并尝试过这个:
[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
似乎是 vs 测试框架中的一个巨大的错误或设计缺陷 :) 正如这里所说的 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 个相同的文字,所以肯定是真的!
回答by Marc Gravell
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
现在你有了你的AreEqual,你应该能够删除它Inconclusive
Anyfailure during a test (not including exceptions that you intentionally handle) is generally terminal, but any assert that passes(like the AreEqualhere) just keeps on running. So the first test passes, then the last line flags it as inconclusive.
测试期间的任何失败(不包括您有意处理的异常)通常都是终端,但任何通过的断言(如此AreEqual处)只会继续运行。所以第一个测试通过,然后最后一行将其标记为不确定。
回答by ChrisF
Even when you've removed the Assert.Inconclusiveyou still might have problems.
即使您已删除 ,Assert.Inconclusive您仍然可能会遇到问题。
You're testing the equality of two floating point numbers and in general with calculated valuesyou'll never get them exactlythe 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.
Assert.AreEqual(expected, actual);在这种情况下您的工作是因为您为两个变量分配了相同的值。
回答by Shikeh
The automatic UnitTest is generated by VS and tells you to create some operations to compare. if you'll comment the last Assert command you will get "Passed" with green mark, but you didn't test it.
You need, as in the comment, "Initialize to an appropriate value" and as in the last Assert "Verify the correctness of this test method".
Initialize the expected and the actual values from where they come from
for example, Expected is the expected value from function Add(x,y) where x=2 and y=3. Actual value should come from the function. in this case:
自动 UnitTest 由 VS 生成,并告诉您创建一些操作进行比较。如果你评论最后一个 Assert 命令,你会得到带有绿色标记的“通过”,但你没有测试它。
您需要,如注释中所述,“初始化为适当的值”和最后一个 Assert 中的“验证此测试方法的正确性”。
初始化预期值和实际值,例如,预期值是函数 Add(x,y) 的预期值,其中 x=2 和 y=3。实际值应该来自函数。在这种情况下:
// Sample - Start
Expected = 2+3;
Actual = Add(2,3);
Assert.AreEqual(expected, actual);
// Sample - End
Hope it helped, i broke few teeth for that... ;-)
希望它有所帮助,我为此折断了几颗牙齿...... ;-)
回答by Jon Skeet
Doesn't that just mean that the AreEqualpassed, which meant it called Assert.Inconclusive, leading to a result of inconclusive?
那不就是说AreEqual通过了,也就是调用了Assert.Inconclusive,导致结果不定?
From the docs:
从文档:
Similar to Fail in that it indicates an assertion is inconclusive without checking any conditions.
与 Fail 类似,它表明断言在不检查任何条件的情况下是不确定的。
If you don't want the result to be inclusive, remove the call to Assert.Inconclusive:)
如果您不希望结果包含在内,请删除对Assert.Inconclusive:)的调用

