使用JaCoCo Maven插件建立代碼覆蓋率報告

這篇博客文章描述了咱們如何使用JaCoCo Maven插件爲單元和集成測試建立代碼覆蓋率報告。java

咱們的構建要求以下:apache

運行測試時,咱們的構建必須爲單元測試和集成測試建立代碼覆蓋率報告。 代碼覆蓋率報告必須在單獨的目錄中建立。換句話說,必須將用於單元測試的代碼覆蓋率報告建立到與用於集成測試的代碼覆蓋率報告不一樣的目錄中。 讓咱們開始吧。編程

配置JaCoCo Maven插件

咱們使用JaCoCo Maven插件有兩個目的:安全

  • 它使咱們能夠訪問JaCoCo運行時代理,該代理記錄了執行覆蓋率數據。
  • 它根據JaCo​​Co運行時代理記錄的執行數據建立代碼覆蓋率報告。
  • 咱們能夠按照如下步驟配置JaCoCo Maven插件:

將JaCoCo Maven插件添加到咱們的POM文件的插件部分。框架

  • 爲單元測試配置代碼覆蓋率報告。
  • 配置代碼覆蓋率報告以進行集成測試。 下面將更詳細地描述這些步驟。

將JaCoCo Maven插件添加到POM文件

經過將如下插件聲明添加到其「 插件」部分,咱們能夠將JaCoCo Maven插件添加到咱們的POM文件中:maven

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.3.201306030806</version>
</plugin>

配置單元測試的代碼覆蓋率報告

咱們能夠經過將兩個執行添加到插件聲明中來爲單元測試配置代碼覆蓋率報告。這些執行方式以下所述:post

  • 第一次執行將建立一個指向JaCoCo運行時代理的屬性。確保執行數據已寫入文件target / coverage-reports / jacoco-ut.exec。將該屬性的名稱設置爲surefireArgLine。運行單元測試時,此屬性的值做爲VM參數傳遞。
  • 運行單元測試後,第二次執行將爲單元測試建立代碼覆蓋率報告。確保從文件target / coverage-reports / jacoco-ut.exec中讀取執行數據,並將代碼覆蓋率報告寫入目錄target / site / jacoco-ut中。

咱們的插件配置的相關部分以下所示:性能

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.3.201306030806</version>
    <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>${project.build.directory}/coverage-reports/jacoco-ut.exec</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>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

讓咱們找出如何爲集成測試配置代碼覆蓋率報告。單元測試

配置集成測試的代碼覆蓋率報告

咱們能夠經過在插件聲明中添加兩個執行來爲集成測試配置代碼覆蓋率報告。這些執行方式以下所述:測試

  • 第一次執行將建立一個指向JaCoCo運行時代理的屬性。確保將執行數據寫入文件target / coverage-reports / jacoco-it.exec。將該屬性的名稱設置爲failsafeArgLine。運行咱們的集成測試時,此屬性的值做爲VM參數傳遞。
  • 建立一個執行,該執行在集成測試運行後爲集成測試建立代碼覆蓋率報告。確保從文件target / coverage-reports / jacoco-it.exec中讀取執行數據,並將代碼覆蓋率報告寫入目錄target / site / jacoco-it。

咱們的插件配置的相關部分以下所示:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.3.201306030806</version>
    <executions>
        <!-- The Executions required by unit tests are omitted. -->
        <!--
           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>${project.build.directory}/coverage-reports/jacoco-it.exec</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>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

如今,咱們已經配置了JaCoCo Maven插件。下一步是配置Maven Surefire插件。讓咱們找出如何作到這一點。

配置Maven Surefire插件

咱們使用Maven Surefire插件運行示例應用程序的單元測試。由於咱們要爲單元測試創​​建代碼覆蓋率報告,因此咱們必須確保在運行單元測試時JaCoCo代理正在運行。咱們能夠經過添加的價值保證本surefireArgLine財產做爲價值argLine配置參數。

Maven Surefire插件的配置以下所示(突出顯示了所需的更改):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <configuration>
        <!-- Sets the VM argument line used when unit tests are run. -->
        <argLine>${surefireArgLine}</argLine>
        <!-- Skips unit tests if the value of skip.unit.tests property is true -->
        <skipTests>${skip.unit.tests}</skipTests>
        <!-- Excludes integration tests when unit tests are run. -->
        <excludes>
            <exclude>**/IT*.java</exclude>
        </excludes>
    </configuration>
</plugin>

咱們快完成了。剩下要作的就是配置Maven Failsafe插件。讓咱們找出如何作到這一點。

配置Maven故障安全插件

咱們的示例應用程序的集成測試由Maven Failsafe插件運行。由於咱們要爲集成測試建立代碼覆蓋率報告,因此咱們必須確保在運行集成測試時JaCoCo代理正在運行。咱們能夠經過將failsafeArgLine屬性的值添加爲argLine配置參數的值來實現。

Maven Failsafe插件的配置以下所示(突出顯示了所需的更改):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.15</version>
    <executions>
        <!--
            Ensures that both integration-test and verify goals of the Failsafe Maven
            plugin are executed.
        -->
        <execution>
            <id>integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <!-- Sets the VM argument line used when integration tests are run. -->
                <argLine>${failsafeArgLine}</argLine>
                <!--
                    Skips integration tests if the value of skip.integration.tests property
                    is true
                -->
                <skipTests>${skip.integration.tests}</skipTests>
            </configuration>
        </execution>
    </executions>
</plugin>

建立代碼覆蓋率報告

如今,咱們已成功完成所需的配置。讓咱們看看如何爲單元測試和集成測試建立代碼覆蓋率報告。

此博客文章的示例應用程序具備三個構建配置文件,下面對此進行了描述:

  • 在開發配置文件開發過程當中使用,這是咱們構建的默認配置文件。當此配置文件處於活動狀態時,僅運行單元測試。

  • 在集成測試配置文件用於運行集成測試。

  • 在全部的測試配置文件用於爲運行單元測試和集成測試。 咱們能夠經過在命令提示符處運行如下命令來建立不一樣的代碼覆蓋率報告:

  • 命令mvn clean test運行單元測試,併爲目錄target / site / jacoco-ut建立單元測試的代碼覆蓋率報告。

  • 命令mvn clean verify -P integration-test運行集成測試,併爲目錄target / site / jacoco-it建立用於集成測試的代碼覆蓋率報告。

  • 命令mvn clean verify -P all-tests運行單元測試和集成測試,併爲單元測試和集成測試建立代碼覆蓋率報告。

技術類文章精選

非技術文章精選

相關文章
相關標籤/搜索