使用JaCoCo統計單元測試代碼覆蓋率


1 JaCoCo介紹

JaCoCo是EclEmma團隊基於多年覆蓋率庫使用經驗總結而研發的一個開源的Java代碼覆蓋率庫。

2 JaCoCo覆蓋率計數器

JaCoCo 包含了多種尺度的覆蓋率計數器(Coverage Counters),包含指令級(Instructions,C0 coverage)、分支(Branches,C1 coverage)、圈復雜度(Cyclomatic Complexity)、行(Lines)、方法(Methods)、類(Classes)。具體可參考 JaCoCo覆蓋率計數器

3 Gradle單模塊接入

在工程的buid.gradle文件中添加如下內容:

//Applying the JaCoCo plugin
plugins {
    id 'jacoco'
}


// Configuring JaCoCo plugin settings
jacoco {
    toolVersion = "0.8.4"
    //reportsDir = file("$buildDir/customJacocoReportDir")
}


//Configuring test task
jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.enabled true
        //html.destination file("${buildDir}/jacocoHtml")
    }
}

check.dependsOn jacocoTestReport

編譯完成后會在 ${buildDir}/build/reports/jacoco下生成報告。

如果在Spring Boot的框架下運行,需要在build.gradle中加下面一段,否則執行jacocoTestReport task會被skipped。

tasks.withType(Test) {
    scanForTestClasses = false
    include "**/*Test.class" 
}

4 Gradle多模塊接入

在父子工程的buid.gradle文件中添加如下內容,注意父和子都要添加:

//Applying the JaCoCo plugin

apply plugin:'jacoco'


// Configuring JaCoCo plugin settings
jacoco {
    toolVersion = "0.8.4"
    //reportsDir = file("$buildDir/customJacocoReportDir")
}


//Configuring test task
jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.enabled true
        //html.destination file("${buildDir}/jacocoHtml")
    }
}

check.dependsOn jacocoTestReport

編譯完成后會在對應的子工程的 ${buildDir}/build/reports/jacoco下生成報告。

5 Maven單模塊接入

在工程的pom.xml文件中添加如下內容:

<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>

執行Run As Maven build:

clean install 

在項目target/site/jacoco目錄下找到index.html文件,即可查看報告。

6 Maven多模塊接入

Maven多模塊是指存在父子關聯的項目,這類項目中存在多個pom.xml文件,父項目pom.xml文件中包括多個<module>標簽,指明它的子模塊有哪些,子項目pom.xml中能繼承父項目配置的依賴,通過<parent>標簽能知道它的父模塊是誰。

6.1 父pom中增加依賴

在多模塊項目中找到父pom.xml文件,添加如下內容:

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

6.2 新建子覆蓋率模塊

該模塊添加所有要進行單元測試的子工程的依賴,例如,新增一個jacoco-coverage的子模塊,在pom.xml文件里添加如下內容:

增加<dependency>指定統計哪些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>

添加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>

6.3 父pom中增加該模塊

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

執行Run As Maven build:

clean install

在子項目jacoco-coverage生成的target/site/jacoco-aggregate目錄下找到index.html文件並打開即可查看報告。

以下給出一個官網提供的報告

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM