What is the color and number beside the line number in cobertura report
Asked Answered
M

1

6

I used mvn cobertura:cobertura to generate this cobertura JUnit test coverage report. Can anyone explain to me what do the numbers beside the line number mean? Thank you.

enter image description here

Megaron answered 21/10, 2016 at 16:21 Comment(0)
A
6

Those numbers correspond to how many times that line was executed during your tests. Using a simple example:

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

    public void methodB(){
        System.out.println("Method b");
    }
}

With some tests:

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

I will get the following report, showing that I constructed my test object twice, and ran each method once: Non ignored test case

If I add an @Ignore annotation on testMethodB, the following report is produced instead, showing that I only constructed my class once, and did not execute lines within methodB when testing: enter image description here

The color is associated with coverage. It will appear red when there is no test that covers that line or branch.

Edit - Regarding your question in the comments, its possible to be missing coverage due to not checking all conditions. For example, consider this method:

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

and this test:

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

you will end up with the following test report. The reason for this is because although you did execute this line in the test(2 times, in fact), I did not test all permutations of my conditional, therefore, the report will show that I am missing coverage.

conditional coverage

Arlinda answered 21/10, 2016 at 17:27 Comment(2)
Why I can see some line I have called for a lot of times, but it still shows red color? Looks like these are all condition statements, does this mean that I did not cover all the conditions? Thanks.Megaron
Updated my answer. It's hard to tell due to the screenshot, but my guess is you are not testing all permutations of your conditionals, so the report is showing missing coverage.Arlinda

© 2022 - 2024 — McMap. All rights reserved.