线路和分支覆盖范围之间的差异 [英] Differences between Line and Branch coverage

查看:160
本文介绍了线路和分支覆盖范围之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Cobertura Maven插件用于我的一个项目.但是我对生成的报告有疑问:

I use the Cobertura Maven plugin for one of my project. But I have a question about the generated report:

行覆盖率和分支覆盖率有什么区别?

What is the difference between line and branch coverage?

推荐答案

行覆盖率衡量您使用了多少条语句(一条语句通常是一行代码,不包括注释,条件等).分支覆盖范围检查您是否对每个条件(如果,while,for)采用了真假分支.您将拥有的分支数量是条件分支的两倍.

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(); 
}

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

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).

这篇关于线路和分支覆盖范围之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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