Use JaCoCo statistical unit test code coverage

1 JaCoCo Introduction

JaCoCo is EclEmma team based on years of coverage of library uses lessons learned and developed an open source Java library code coverage.

2 Maven single access module

Add the following in the project's pom.xml file:

<build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.4</version>
          <executions>
             <execution>
                 <id>jacoco-initialize</id>
                 <goals>
                    <goal>prepare-agent</goal>
                  </goals>
              </execution>
               <execution>
                 <id>jacoco-site</id>
                 <phase>package</phase>
                 <goals>
                    <goal>report</goal>
                  </goals>
              </execution>
            </executions>
      </plugin>
    </plugins>
</build>

Perform Run As Maven build:

clean install 

Find index.html file in the project target / site / jacoco directory, you can view the report.

3 Maven multiple access module

Maven multi-module program refers to the presence Sons associated plurality of such projects pom.xml file exists, the parent project pom.xml file comprises a plurality of <module> tag, which indicates which sub-modules, sub pom. xml inherit the parent project configuration dependent, by <parent> tag can know who its parent module Yes.

3.1 parent pom-dependent increase

Find parent pom.xml file in a multi-module project, add the following:

<build>
   <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.4</version>
      </plugin>
    </plugins>
   </pluginManagement>
</build>

3.2 New sub-module coverage

The module adds all dependent on project units to be tested, for example, add a jacoco-coverage of sub-module, add the following in the pom.xml file:

Increase <dependency> specify which statistics module

 <dependencies>
                <dependency>
                     <groupId>org.sonatype.mavenbook.multi</groupId>
                     <artifactId>simple-weather</artifactId>
                     <version>1.0.0-SNAPSHOT</version>
                </dependency>
                <dependency>
                     <groupId>org.sonatype.mavenbook.multi</groupId>
                     <artifactId>simple-webapp/artifactId>
                     <version>1.0.0-SNAPSHOT</version>
                </dependency>
 </dependencies>

Add jacoco-maven-plugin

<build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.4</version>
          <executions>
             <execution>
                 <id>jacoco-initialize</id>
                 <goals>
                    <goal>prepare-agent</goal>
                  </goals>
              </execution>
               <execution>
                 <phase>verify</phase>
                 <goals>
                    <goal>report-aggregate</goal>
                  </goals>
              </execution>
            </executions>
      </plugin>
    </plugins>
</build>

3.3 pom increase the parent module

<modules>
  <module>simple-weather</module>
  <module>simple-webapp</module>
  <module>jacoco-coverage</module>
</modules>

Perform Run As Maven build:

clean install

Index.html file is found in sub jacoco-coverage generated target / site under / jacoco-aggregate directory and open to view reports.

The following gives an official website provides reports :

 

Guess you like

Origin www.cnblogs.com/ycyzharry/p/10992416.html