Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Maven unit test code coverage work

In Eclipse, I have used EcLEmma to see the unit test code coverage. Which worked fine. Therefore I have tried to use the JaCoCo plugin for Maven to see the same report with Surefire from Maven build, or even better, with a certain profile, or in the site cycle. Without success. All suggested solutions here didn't work for me.

What is the best way to get a unit test code coverage report (with surefire)?

[Edit] to be more specific why jacoco failed for me.... as I got always the Skipping JaCoCo execution due to missing execution data

from the pom in the properties

    <jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
    <jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>

in the Build section

        <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.jacoco</groupId>
                                    <artifactId>jacoco-maven-plugin</artifactId>
                                    <versionRange>${jacoco.version}</versionRange>
                                    <executions>
                                        <!-- Prepares the property pointing to the JaCoCo runtime agent 
                                            which is passed as VM argument when Maven the Surefire plugin is executed. -->
                                        <execution>
                                            <id>pre-unit-test</id>
                                            <goals>
                                                <goal>prepare-agent</goal>
                                            </goals>
                                            <configuration>
                                                <!-- Sets the path to the file which contains the execution 
                                                    data. -->
                                                <destFile>${jacoco.ut.execution.data.file}</destFile>
                                                <!-- Sets the name of the property containing the settings for 
                                                    JaCoCo runtime agent. -->
                                                <propertyName>surefireArgLine</propertyName>
                                            </configuration>
                                        </execution>
                                        <!-- Ensures that the code coverage report for unit tests is created 
                                            after unit tests have been run. -->
                                        <execution>
                                            <id>post-unit-test</id>
                                            <phase>test</phase>
                                            <goals>
                                                <goal>report</goal>
                                            </goals>
                                            <configuration>
                                                <!-- Sets the path to the file which contains the execution 
                                                    data. -->
                                                <dataFile>${jacoco.ut.execution.data.file}</dataFile>
                                                <!-- Sets the output directory for the code coverage report. -->
                                                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                                            </configuration>
                                        </execution>
                                        <!-- Prepares the property pointing to the JaCoCo runtime agent 
                                            which is passed as VM argument when Maven the Failsafe plugin is executed. -->
                                        <execution>
                                            <id>pre-integration-test</id>
                                            <phase>pre-integration-test</phase>
                                            <goals>
                                                <goal>prepare-agent</goal>
                                            </goals>
                                            <configuration>
                                                <!-- Sets the path to the file which contains the execution 
                                                    data. -->
                                                <destFile>${jacoco.it.execution.data.file}</destFile>
                                                <!-- Sets the name of the property containing the settings for 
                                                    JaCoCo runtime agent. -->
                                                <propertyName>failsafeArgLine</propertyName>
                                            </configuration>
                                        </execution>
                                        <!-- Ensures that the code coverage report for integration tests 
                                            after integration tests have been run. -->
                                        <execution>
                                            <id>post-integration-test</id>
                                            <phase>post-integration-test</phase>
                                            <goals>
                                                <goal>report</goal>
                                            </goals>
                                            <configuration>
                                                <!-- Sets the path to the file which contains the execution 
                                                    data. -->
                                                <dataFile>${jacoco.it.execution.data.file}</dataFile>
                                                <!-- Sets the output directory for the code coverage report. -->
                                                <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
                                            </configuration>
                                        </execution>
                                    </executions>
                                </pluginExecutionFilter>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

was my last try but the pom becomes bigger and bigger without any result

which failes with

configuring report plugin org.apache.maven.plugins:maven-jxr-plugin:2.3

configuring report plugin org.jacoco:jacoco-maven-plugin:0.7.5.201505241946

Skipping JaCoCo execution due to missing execution data file:......\target\jacoco.exec

Skipping JaCoCo execution due to missing execution data file:......\target\jacoco-it.exec

.... => long project path

like image 715
user3732793 Avatar asked Mar 24 '16 11:03

user3732793


People also ask

How do I get Maven code coverage?

To get code coverage reports in a Maven project, we first need to set up the JaCoCo Maven plugin for that project. By integrating the JaCoCo plugin, the results of the code coverage analysis can be reviewed as an HTML report. The current version of the JaCoCo-Maven plugin can be downloaded from the MVN Repository.

How do you use JaCoCo for code coverage in Maven?

Run the mvn package command. The package command will invoke the test phase during the packaging of the project jar or war file. In the test phase, JaCoCo agent will initialize and run the code coverage analysis while the tests are executed.

How do I get my JaCoCo code coverage report?

Step 4: To get you code coverage report navigate to the target > site > jacoco > index.


2 Answers

Thanks user3732793

Personnaly, I only needed to add this to my pom.xml

<project>
...

    <dependencies>
        ...
    </dependencies>

    <build>
        <plugins>
            ...
            <!-- Code Coverage report generation -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>generate-code-coverage-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Then I am running

mvn test

and I get the HTML report under ./target/site/jacoco/*.html

like image 159
Julien Avatar answered Oct 22 '22 16:10

Julien


As always the solution is easy after reading the documentation which provides example poms jacoco documentation.

This profile:

    <profile>
        <id>test</id>
        <properties>
            <env>test</env>
            <gebEnv>test</gebEnv>
            <jacoco.skip>false</jacoco.skip>
            <maven.test.skip>false</maven.test.skip>
            <skip.unit.tests>false</skip.unit.tests>
        </properties>
    </profile>

This in the build section:

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

And this in the reporting section:

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
        </plugin>

Than this does all:

mvn clean install site -P test
like image 43
user3732793 Avatar answered Oct 22 '22 17:10

user3732793