使用JaCoCo Maven插件创建代码覆盖率报告
这篇博客文章描述了我们如何使用JaCoCo Maven插件为单元和集成测试创建代码覆盖率报告。
我们的构建要求如下:
运行测试时,我们的构建必须为单元测试和集成测试创建代码覆盖率报告。
代码覆盖率报告必须在单独的目录中创建。换句话说,必须将用于单元测试的代码覆盖率报告创建到与用于集成测试的代码覆盖率报告不同的目录中。
让我们开始吧。
配置JaCoCo Maven插件
我们使用JaCoCo Maven插件有两个目的:
- 它使我们可以访问JaCoCo运行时代理,该代理记录了执行覆盖率数据。
- 它根据JaCoCo运行时代理记录的执行数据创建代码覆盖率报告。
- 我们可以按照以下步骤配置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运行单元测试和集成测试,并为单元测试和集成测试创建代码覆盖率报告。
技术类文章精选
- java一行代码打印心形
- Linux性能监控软件netdata中文汉化版
- 接口测试代码覆盖率(jacoco)方案分享
- 性能测试框架
- 如何在Linux命令行界面愉快进行性能测试
- 图解HTTP脑图
- 将swagger文档自动变成测试代码
- 五行代码构建静态博客
- 基于java的直线型接口测试框架初探
- JUnit中用于Selenium测试的中实践
非技术文章精选
- 为什么选择软件测试作为职业道路?
- 写给所有人的编程思维
- 成为优秀自动化测试工程师的7个步骤
- 手动测试存在的重要原因
- 成为自动化测试的7种技能
- 自动化和手动测试,保持平衡!
- 自动化测试生命周期
- 如何在DevOps引入自动化测试
使用JaCoCo Maven插件创建代码覆盖率报告的更多相关文章
- 使用Eclipse自带的Maven插件创建Web项目时报错:
问题描述: 使用Eclipse自带的Maven插件创建Web项目时报错: Could not resolve archetype org.apache.maven.archetypes:maven-a ...
- Windows10下安装Maven以及Eclipse安装Maven插件 + 创建Maven项目
在官网下载Maven http://maven.apache.org/download.cgi 下载下来后加压缩,将apache-maven-3.5.4文件夹复制到想要存放它的位置,我放在了 ...
- eclipse maven插件创建maven项目
1.下载好maven压缩包http://maven.apache.org/ ,解压后放到想要安装的目录,如E:/server/maven,然后配置好maven环境变量,找到maven安装目录下conf ...
- Maven学习之(二)Maven插件创建项目
现在这里是已经在eclipse上成功的安装了Maven的插件后的操作,简单的创建一个项目, 同时实现Maven功能. 前提条件: 1.JDK 2.maven安装好 3.eclipse上maven插件安 ...
- Maven学习之(三)Maven插件创建web项目
这里来记录一下用eclipse 创建maven 的web项目. 添加jar依赖. <project xmlns="http://maven.apache.org/POM/4.0.0&q ...
- Maven(六)Eclipse使用Maven插件创建项目
1. 创建Maven版Java工程 1.1 具体步骤 1.2 更改默认JDK版本 默认JDK版本过低 可以通过配置setting.xml来更改JDK版本 加入如下代码 <profile> ...
- Maven学习之(四)Maven插件创建web项目
另一种maven web项目的创建. 创建出来的目录是这样的,此时试一下,不能加入到tomcat中去启动. 这里要将项目转化为web项目. 右键->项目 选中下面的动态web项目,然后OK 此时 ...
- eclipse——Maven插件创建java工程
目录结构如下 注意默认JDK为1.5 更改默认JDK 方式一 右键工程 选中JRE1.5 Remove 双击JRE System Library 点击Finish 更改完成 方式二 配置maven ...
- jacoco+maven生成单元测试覆盖率报告
参考:https://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-in ...
随机推荐
- Python的map方法的应用
Map方法,第一个参数要写一个匿名函数表达式,或者是一个函数引用,第二个第三个往后都是表达式用到的参数,参数一般是可迭代的 1.比如下面这个map方法,两个参数,第一个 lambda x: x*x是匿 ...
- java之JavaBean
JavaBean是一种java语言编写而成的可重用组件. 所谓JavaBean,是指符合以下标准的java类: 类是公共的: 有一个无参的公共构造器: 有属性,属性一般是私有的,且有对应的set.ge ...
- Django入门必知必会操作
一.Django基础必备三件套 HttpRseponse 内部传入一个字符串参数,返回给浏览器. 在app目录下的views.py添加函数,添加函数之前必须在urls.py添加函数对应关系,否则访问不 ...
- Web前端基础(13):JavaScript(七)
1. BOM JavaScript基础分为三部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:文档对象模型,操作网页上的元 ...
- Scrapy中response属性以及内容提取
一.属性 url :HTTP响应的url地址,str类型 status:HTTP响应的状态码, int类型 headers :HTTP响应的头部, 类字典类型, 可以调用get或者getlist方法对 ...
- 清新简约风格毕业论文答辩PPT模板推荐
不管是学生还是老师,应该经常会需要学生答辩的PPT模板,今天给大家推荐织梦58的学生答辩ppt模板. 模版来源:http://ppt.dede58.com/gongzuohuibao/26494.ht ...
- 利用 Docker Compose 搭建 SpringBoot 运行环境(超详细步骤和分析)
0.前言 相信点进来看这篇文章的同学们已经对 Docker Dompose 有一定的了解了,下面,我们拿最简单的例子来介绍如何使用 Docker Compose 来管理项目. 本文例子: 一个应用服务 ...
- [20191119]探究ipcs命令输出2.txt
[20191119]探究ipcs命令输出2.txt --//继续上午的测试:http://blog.itpub.net/267265/viewspace-2664758/=>[20191119] ...
- 微信小程序开发者工具构建npm提示没找到node_modules目录
一.官网给的文档写的不够充分,需要你充分理解npm的使用方法,才能明白的: 二.第一步:先在你电脑上安装npm 参考下面文章 https://www.cnblogs.com/zmdComeOn/p/1 ...
- Django—版本和环境的搭建
1.安装 virtualenv virtualenvwrapper [root@localhost ~]# pip install virtualenv virtualenvwrapper 2.查找 ...