jacoco generates single test coverage report

1, About jacoco

jacoco is an open source coverage tool, and its development language is java. It can be embedded in ant and maven, used as Eclipse Plug-in, used as java agent probe to monitor java programs, etc.

Many third-party tools provide the integration of jacoco, such as sonar, jenkins and so on.

jacoco includes a variety of scale coverage counters, including instruction level coverage (c0coverage), branches (c1coverage), cyclomatic complexity, Lines, non abstract methods, and classes. The meanings are as follows:

  • Line coverage: measures whether each line of code of the program under test is executed, and determines whether at least one instruction in the standard line is executed.
  • Class coverage: measures whether class files are executed.
  • Branch coverage: measure the branch coverage of if and switch statements, calculate the total number of branches in a method, and determine the number of branches executed and not executed.
  • Method coverage: measures the method execution of the program under test. Whether to execute depends on whether at least one instruction in the method is executed.
  • Instruction coverage: the counting unit is a single java binary code instruction. The instruction coverage provides information about whether the code is executed, and measures the completely independent source code format.
  • Cyclomatic complexity: in (linear) combination, calculate the minimum number of all possible paths in a method. The missing complexity also means that the test case does not completely cover this module.

2, jacoco and maven integration

2.1 add parameters to MVN command

When executing mvn command, add "org. Jacoco: jacoco Maven plugin: prepare agent" parameter. Example:

mvn clean test org.jacoco:jacoco-maven-plugin:0.8.5:prepare-agent install -Dmaven.test.failure.ignore=true

Among them, jacoco Maven plugin is followed by the version of jacoco. "- Dmaven.test.failure.ignore=true" is recommended. Otherwise, if the unit test fails, it will be interrupted directly and the. exec file will not be generated.

After executing the above command, a jacoco.exec file will be generated under the target directory of the current directory, which is the coverage file.

In general, this method is relatively simple and convenient for integration with jenkins.

2.2 using the jacoco plug-in in the pom file

First, you need to add the dependency of jacoco:

    <properties>
        <jacoco.version>0.8.5</jacoco.version>
    </properties>
    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco.version}</version>
        <scope>test</scope>
    </dependency>

Next, we need to configure the jacoco Maven plugin and Maven surefire plugin contents:

        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>
                        -javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}-runtime.jar=destfile=${project.basedir}/target/coverage-reports/jacoco-unit.exec
                    </argLine>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
            <!--Check plug-in configuration for code coverage-->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.5</version>
                <configuration>
                    <!--Specify build.exec Storage location of documents-->
                    <destFile>target/coverage-reports/jacoco-unit.exec</destFile>
                    <!--Jacoco It's based on.exec The file generates the final report, so you need to specify.exec Storage path of-->
                    <dataFile>target/coverage-reports/jacoco-unit.exec</dataFile>
                    <includes>
                        <include>**/service/**</include>
                    </includes>
                    <!-- rules Which specifies the override rule -->
                    <rules>
                        <rule implementation="org.jacoco.maven.RuleConfiguration">
                            <element>BUNDLE</element>
                            <limits>  
                                <!-- Specify method override to 50% -->
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>METHOD</counter>
                                    <value>COVEREDRATIO</value>
                                    <minimum>0.50</minimum>
                                </limit>
                                <!-- Specify branch coverage to 50% -->
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>BRANCH</counter>
                                    <value>COVEREDRATIO</value>
                                    <minimum>0.50</minimum>
                                </limit>
                                <!-- Specify class coverage to 100%,No class can be lost -->
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>CLASS</counter>
                                    <value>MISSEDCOUNT</value>
                                    <maximum>0</maximum>
                                </limit>
                            </limits>
                        </rule>
                    </rules>
                </configuration>
                <executions>
                    <execution>
                        <id>pre-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

The value of the < includes > or < excludes > tag should be the classpath (not the package name) of the compiled class relative to the directory / classes /, which specifies which classes need unit testing.

In addition, maven's test classes need to be named according to the corresponding specifications. Otherwise, test classes cannot be run, test reports and coverage reports cannot be generated. jacoco uses the maven surefire plugin plug-in. Its default test class name specification is:

  • Test*.java: Java class starting with test;
  • *Test.java: Java class ending with test;
  • *TestCase.java: a Java class ending in TestCase;

Or you can customize the test class in pom:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<configuration>
		<includes>
			<include>**/*Tests.java</include>
			<include>**/*Test.java</include>
		</includes>
		<excludes>
			<exclude>**/Abstract*.java</exclude>
		</excludes>
	</configuration>
</plugin>

< Rules > specify filter rules.

Then run mvn test to generate index.html, that is, coverage report



Recommended reading:
  1. ant integrated jacoco: http://eclemma.org/jacoco/trunk/doc/ant.html
  2. eclipse uses jacoco: http://www.eclemma.org/

Tags: Java Maven Eclipse jenkins

Posted on Thu, 07 May 2020 04:40:01 -0400 by MasterHernan