【问题标题】:What is the color and number beside the line number in cobertura reportcobertura报告中行号旁边的颜色和数字是什么
【发布时间】:2016-10-21 16:21:21
【问题描述】:

我使用 mvn cobertura:cobertura 来生成这个 cobertura JUnit 测试覆盖率报告。谁能向我解释一下行号旁边的数字是什么意思?谢谢。

【问题讨论】:

    标签: maven testing junit code-coverage cobertura


    【解决方案1】:

    这些数字对应于测试期间该行执行的次数。用一个简单的例子:

    public class MyClass {
        public void methodA(){
            System.out.println("Method a");
        }
    
        public void methodB(){
            System.out.println("Method b");
        }
    }
    

    通过一些测试:

    public class MyClassTest {
    
        @Test
        public void testMethodA(){
            final MyClass x = new MyClass();
            x.methodA();
        }
    
        @Test
        public void testMethodB(){
            final MyClass x = new MyClass();
            x.methodB();
        }
    }
    

    我将得到以下报告,表明我构建了我的测试对象两次,并且每个方法运行了一次:

    如果我在testMethodB 上添加@Ignore 注释,则会生成以下报告,表明我只构建了一次类,​​并且在测试时没有执行methodB 内的行:

    颜色与覆盖率相关。当没有覆盖该行或分支的测试时,它将显示为红色。

    编辑 - 关于您在 cmets 中的问题,由于未检查所有条件,可能会丢失覆盖范围。例如,考虑这种方法:

    public void methodB(final boolean testOne, final boolean testTwo){
        if(testOne || testTwo){
            System.out.println("Method b");
        }
        System.out.println("Done");
    }
    

    还有这个测试:

    @Test
        public void testMethodB(){
            final MyClass x = new MyClass();
            x.methodB(true, false);
            x.methodB(true, true);
        }
    

    您最终会得到以下测试报告。这是因为尽管您在测试中确实执行了这一行(实际上是 2 次),但我没有测试条件的所有排列,因此,报告将显示我缺少覆盖率。

    【讨论】:

    • 为什么我可以看到我已经调用了很多次的线路,但它仍然显示为红色?看起来这些都是条件语句,这是否意味着我没有涵盖所有条件?谢谢。
    • 更新了我的答案。由于屏幕截图,很难判断,但我猜您没有测试条件的所有排列,因此报告显示缺少覆盖范围。
    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 2023-03-04
    • 2018-02-19
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多