Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Cobertura work with JUnit?

I can't understand how Cobertura cooperates with JUnit. As I understood cobertura modifies compiled byte code and inserts in this byte code its own commands. Ok. After that we run Junit framework and give it our tests to run. Could anyone explain at what points cobertura gets the information which of its commands were executed?

like image 946
Pavel_K Avatar asked Feb 16 '16 10:02

Pavel_K


People also ask

How does cobertura check code coverage?

Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage.

How does cobertura integrate with Eclipse?

Installation. Step 2: Select eCobertura Code Coverage, click “next”, and then follow the steps in the installation wizard. Now that eCobertura is installed, restart Eclipse and show the coverage session view under Windows → Show View → Other → Cobertura.

What is cobertura Maven plugin used for?

The report generated by this plugin is the result of executing the Cobertura tool against your compiled classes to help you determine how well the unit testing and integration testing efforts have been, and can then be used to identify which parts of your Java program are lacking test coverage.


2 Answers

Cobertura uses ASM which is a general purpose bytecode manipulation and analysis framework. On every line of java code there are 3 lines added to the existing classes to count things for the report it produces. When Cobertura is included in your classpath and configured correctly and you execute your unit tests, it will produce a datafile called cobertura.ser which is used to produce an xml or html report.

Basic usage: with Maven: http://www.mojohaus.org/cobertura-maven-plugin/usage.html

like image 168
Griff Avatar answered Sep 24 '22 15:09

Griff


Cobertura monitors tests by instrumenting the bytecode with extra statements to log which lines are and are not being reached as the test suite executes.

Cobertura calculates coverage both by the number of lines tested and by the number of branches tested. For a first pass, the difference between these two is not hugely important. Cobertura also calculates the average McCabe's cyclomatic complexity for the class.

If using maven this can be configured in POM:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <formats>
                <format>html</format>
                <format>xml</format>
            </formats>
        </configuration>
    </plugin>

If using ANT it can be configured with the taskdef statement in the build.xml file:

   <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>

Reference for ant-cobertura integration can be found at https://github.com/cobertura/cobertura/wiki/Ant-Task-Reference

like image 28
neethi Avatar answered Sep 25 '22 15:09

neethi