这篇博客文章描述了我们如何使用 JaCoCo Maven 插件为单元和集成测试创建代码覆盖率报告。

我们的构建要求如下:

运行测试时,我们的构建必须为单元测试和集成测试创建代码覆盖率报告。
代码覆盖率报告必须在单独的目录中创建。换句话说,必须将用于单元测试的代码覆盖率报告创建到与用于集成测试的代码覆盖率报告不同的目录中。
让我们开始吧。

配置 JaCoCo Maven 插件

我们使用 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>

配置单元测试的代码覆盖率报告

我们可以通过将两个执行添加到插件声明中来为单元测试配置代码覆盖率报告。这些执行方式如下所述:

我们的插件配置的相关部分如下所示:

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

让我们找出如何为集成测试配置代码覆盖率报告。

配置集成测试的代码覆盖率报告

我们可以通过在插件声明中添加两个执行来为集成测试配置代码覆盖率报告。这些执行方式如下所述:

我们的插件配置的相关部分如下所示:

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

创建代码覆盖率报告

现在,我们已成功完成所需的配置。让我们看看如何为单元测试和集成测试创建代码覆盖率报告。

此博客文章的示例应用程序具有三个构建配置文件,下面对此进行了描述:

技术类文章精选

非技术文章精选


↙↙↙阅读原文可查看相关链接,并与作者交流