单元测试代码覆盖率-您有100%的覆盖率吗? [英] Unit testing code coverage - do you have 100% coverage?

查看:472
本文介绍了单元测试代码覆盖率-您有100%的覆盖率吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您的单元测试是否构成100%的代码覆盖率?是或否,以及为什么或为什么不这样做。

Do your unit tests constitute 100% code coverage? Yes or no, and why or why not.

推荐答案

否出于几个原因:


  • 达到100%的覆盖率确实很昂贵,而获得好处的90%或95%并不明显。

  • 即使覆盖率达到100%,您的代码也不是完美的。看一下这种方法(实际上,这取决于您在谈论哪种类型的承保范围-分支承保范围线路承保范围 ...):
  • b $ b
  • It is really expensive to reach the 100% coverage, compared to the 90% or 95% for a benefit that is not obvious.
  • Even with 100% of coverage, your code is not perfect. Take a look at this method (in fact it depends on which type of coverage you are talking about - branch coverage, line coverage...):


public static String foo(boolean someCondition) {
    String bar = null;
    if (someCondition) {
        bar = "blabla";
    }
    return bar.trim();
}

和单元测试:

assertEquals("blabla", foo(true));

测试将成功,您的代码覆盖率为100%。但是,如果添加另一个测试:

The test will succeed, and your code coverage is 100%. However, if you add another test:

assertEquals("blabla", foo(false));

然后您将获得 NullPointerException 。而且,由于您第一次测试达到100%的水平,因此您不一定要编写第二个测试!

then you will get a NullPointerException. And as you were at 100% with the first test, you would have not necessarily write the second one!

通常,我认为关键 >代码必须覆盖几乎100%,而其他代码可以覆盖85-90%

Generally, I consider that the critical code must be covered at almost 100%, while the other code can be covered at 85-90%

这篇关于单元测试代码覆盖率-您有100%的覆盖率吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆