线路覆盖范围和分支覆盖范围之间的差异

发布于 2024-12-17 23:08:35 字数 42 浏览 2 评论 0原文

Cobertura Maven 中的线路覆盖率和分支覆盖率有什么区别?

What is the difference between line and branch coverage in Cobertura Maven?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

病女 2024-12-24 23:08:35

行覆盖率衡量您采用了多少条语句(一条语句通常是一行代码,不包括注释、条件等)。分支覆盖检查您是否为每个条件(if、while、for)选择了 true 和 false 分支。您的分支数量将是条件语句的两倍。

你为什么关心?考虑以下示例:

public int getNameLength(boolean isCoolUser) {
    User user = null;
    if (isCoolUser) {
        user = new John(); 
    }
    return user.getName().length(); 
}

如果您在将 isCoolUser 设置为 true 的情况下调用此方法,则将获得 100% 的语句覆盖率。听起来不错吗?不,如果您使用 false 调用,将会出现空指针。但是,在第一种情况下,您的分支覆盖率为 50%,因此您可以看到测试中(通常是代码中)缺少某些内容。

Line coverage measures how many statements you took (a statement is usually a line of code, not including comments, conditionals, etc). Branch coverages checks if you took the true and false branch for each conditional (if, while, for). You'll have twice as many branches as conditionals.

Why do you care? Consider the example:

public int getNameLength(boolean isCoolUser) {
    User user = null;
    if (isCoolUser) {
        user = new John(); 
    }
    return user.getName().length(); 
}

If you call this method with isCoolUser set to true, you get 100% statement coverage. Sounds good? NOPE, there's going to be a null pointer if you call with false. However, you have 50% branch coverage in the first case, so you can see there is something missing in your testing (and often, in your code).

╄→承喏 2024-12-24 23:08:35

将此代码作为一个简化的示例:

if(cond) {
    line1();
    line2();
    line3();
    line4();
} else {
    line5();
}

如果您的测试仅执行 cond 为 true 并且从不运行 else 分支,则您将得到:

  • 5 行中的 4 行覆盖
  • 2 行中的 1 行覆盖的分支

另外,Cobertura report 本身在单击列标题时引入了一些不错的弹出帮助工具提示:

行覆盖率 - 此测试运行执行的行的百分比。

分支覆盖率 - 此测试运行执行的分支的百分比。

Take this code as a simplified example:

if(cond) {
    line1();
    line2();
    line3();
    line4();
} else {
    line5();
}

If your test only exercises the cond being true and never runs the else branch you have:

  • 4 out of 5 lines covered
  • 1 out of 2 branches covered

Also Cobertura report itself introduces some nice pop-up help tooltips when column header is clicked:

Line Coverage - The percent of lines executed by this test run.

Branch Coverage - The percent of branches executed by this test run.

天冷不及心凉 2024-12-24 23:08:35
if(cond){
    //branch 1
}else{  
    //branch 2
}

您需要解决分支 1 和分支 2 中的所有线路,才能获得 LineCoverage 和 BranchCoverage 的 100% 覆盖率。

如果您错过了其他任何内容,您将获得一半的分支机构覆盖。
如果您在 if 和 else 中错过了行数中的任何内容,您将获得 100% 的 BranchCoverage,但行覆盖率不是 100%。

希望这有帮助。

if(cond){
    //branch 1
}else{  
    //branch 2
}

You need to address all lines is branch 1 and branch 2 to get 100% coverage for both LineCoverage and BranchCoverage.

If you at all miss anything in else, you will get half of branch coverage.
If you have missed anything in # of lines in both if and else, you will get BranchCoverage of 100% but not 100% with line coverage.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文