即使我覆盖了100%的代码,我的代码仍可以包含哪些类型的错误? [英] What type of errors could my code still contain even if I have 100% code coverage?
问题描述
即使我具有100%的代码覆盖率,我的代码仍可以包含哪些类型的错误?我正在寻找此类错误的具体示例或指向具体示例的链接。
What type of errors could my code still contain even if I have 100% code coverage? I'm looking for concrete examples or links to concrete examples of such errors.
推荐答案
具有100%的代码覆盖率并不像人们想象的那么大。考虑一个简单的例子:
Having 100% code coverage is not that great as one may think of it. Consider a trival example:
double Foo(double a, double b)
{
return a / b;
}
即使是单个单元测试也会将此方法的代码覆盖率提高到100%,但上述单元测试不会告诉我们哪些代码有效,哪些代码无效。这可能是完全有效的代码,但是没有测试边缘条件(例如,当 b
为 0.0
时),单元测试为
Even a single unit test will raise code coverage of this method to 100%, but the said unit test will not tell us what code is working and what code is not. This might be a perfectly valid code, but without testing edge conditions (such as when b
is 0.0
) unit test is inconclusive at best.
代码覆盖率仅告诉我们单元测试执行了什么,而不是告诉我们是否正确执行了。这是一个重要的区别。仅仅由于一行测试是由单元测试执行的,并不一定意味着该行代码按预期工作。
Code coverage only tells us what was executed by our unit tests, not whether it was executed correctly. This is an important distinction to make. Just because a line of code is executed by a unit test, does not necessarily mean that that line of code is working as intended.
收听此有趣的讨论。
这篇关于即使我覆盖了100%的代码,我的代码仍可以包含哪些类型的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!