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

2{icon} {views}

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

我們的構建要求如下:

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

配置JaCoCo Maven插件

我們使用JaCoCo Maven插件有兩個目的:

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

將JaCoCo Maven插件添加到我們的POM文件的插件部分。

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

將JaCoCo Maven插件添加到POM文件

通過將以下插件聲明添加到其“ 插件”部分,我們可以將JaCoCo Maven插件添加到我們的POM文件中:

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

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

我們可以通過將兩個執行添加到插件聲明中來為單元測試配置代碼覆蓋率報告。這些執行方式如下所述:

  • 第一次執行將創建一個指向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運行單元測試和集成測試,併為單元測試和集成測試創建代碼覆蓋率報告。

技術類文章精選

非技術文章精選

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理
【其他文章推薦】

※想知道網站建置網站改版該如何進行嗎?將由專業工程師為您規劃客製化網頁設計後台網頁設計

※不管是台北網頁設計公司台中網頁設計公司,全省皆有專員為您服務

※Google地圖已可更新顯示潭子電動車充電站設置地點!!

※帶您來看台北網站建置台北網頁設計,各種案例分享

小三通物流營運型態?

※快速運回,大陸空運推薦?