前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用JaCoCo Maven插件创建代码覆盖率报告

使用JaCoCo Maven插件创建代码覆盖率报告

作者头像
FunTester
发布2019-11-19 22:09:38
1.7K0
发布2019-11-19 22:09:38
举报
文章被收录于专栏:FunTesterFunTester

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

我们的构建要求如下:

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

配置JaCoCo Maven插件

我们使用JaCoCo Maven插件有两个目的:

  • 它使我们可以访问JaCoCo运行时代理,该代理记录了执行覆盖率数据。
  • 它根据JaCoCo运行时代理记录的执行数据创建代码覆盖率报告。
  • 我们可以按照以下步骤配置JaCoCo Maven插件:

将JaCoCo Maven插件添加到我们的POM文件的插件部分。

  • 为单元测试配置代码覆盖率报告。
  • 配置代码覆盖率报告以进行集成测试。下面将更详细地描述这些步骤。

将JaCoCo Maven插件添加到POM文件

通过将以下插件声明添加到其“ 插件”部分,我们可以将JaCoCo Maven插件添加到我们的POM文件中:

代码语言:javascript
复制
<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中。

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

代码语言:javascript
复制
<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。

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

代码语言:javascript
复制
<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插件的配置如下所示(突出显示了所需的更改):

代码语言:javascript
复制
<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插件的配置如下所示(突出显示了所需的更改):

代码语言:javascript
复制
<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运行单元测试和集成测试,并为单元测试和集成测试创建代码覆盖率报告。

技术类文章精选

非技术文章精选

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 FunTester 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 配置JaCoCo Maven插件
  • 将JaCoCo Maven插件添加到POM文件
  • 配置单元测试的代码覆盖率报告
  • 配置集成测试的代码覆盖率报告
  • 配置Maven Surefire插件
  • 配置Maven故障安全插件
  • 创建代码覆盖率报告
  • 技术类文章精选
  • 非技术文章精选
相关产品与服务
应用性能监控
应用性能监控(Application Performance Management,APM)是一款应用性能管理平台,基于实时多语言应用探针全量采集技术,为您提供分布式性能分析和故障自检能力。APM 协助您在复杂的业务系统里快速定位性能问题,降低 MTTR(平均故障恢复时间),实时了解并追踪应用性能,提升用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档