maven jacoco

当我开始使用Java 7时,我立即注意到Cobertura Maven插件不支持它 。 这对我来说是个大问题,因为我每天都使用代码覆盖率报告。 我做了一些研究,发现了JaCoCo代码覆盖库 。 看起来很有趣,我决定试一试。

问题在于配置它确实很困难,并且花费了大量时间。 我阅读了许多教程,只是发现其中给出的说明对我不起作用。 然后我遇到了这个博客文章 ,一切都准备就绪。

尽管该博客文章对我来说非常有价值,但还是有点含糊。 我觉得对JaCoCo Maven插件的用法进行更详细的解释很有价值。

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

我们的构建要求如下:

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

让我们开始吧。

注意 :这篇博客文章的示例应用程序基于我的博客文章“ Maven集成测试”的示例应用程序。 如果尚未阅读,建议您在阅读本博客文章之前先阅读它。

配置JaCoCo Maven插件

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

  1. 它使我们可以访问JaCoCo运行时代理 ,该代理记录了执行覆盖率数据。
  2. 它根据JaCo​​Co运行时代理记录的执行数据创建代码覆盖率报告。

我们可以按照以下步骤配置JaCoCo Maven插件:

  1. 将JaCoCo Maven插件添加到我们的POM文件的插件部分。
  2. 为单元测试配置代码覆盖率报告。
  3. 配置代码覆盖率报告以进行集成测试。

下面将更详细地描述这些步骤。

将JaCoCo Maven插件添加到POM文件

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

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

让我们继续研究如何为单元测试配置代码覆盖率报告。

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

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

  1. 第一次执行将创建一个指向JaCoCo运行时代理的属性。 确保将执行数据写入文件target / coverage-reports / jacoco-ut.exec 。 将该属性的名称设置为surefireArgLine 。 运行单元测试时,此属性的值作为VM参数传递。
  2. 运行单元测试后,第二次执行将为单元测试创建代码覆盖率报告 。 确保从文件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 whichis 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 settingsfor JaCoCo runtime agent.--><propertyName>surefireArgLine</propertyName></configuration></execution><!--Ensures that the code coverage report for unit tests is created afterunit 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>

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

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

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

  1. 第一次执行将创建一个指向JaCoCo运行时代理的属性。 确保将执行数据写入文件target / coverage-reports / jacoco-it.exec 。 将属性的名称设置为failsafeArgLine 。 在运行集成测试时,此属性的值作为VM参数传递。
  2. 运行执行集成测试后,创建一个执行,该执行将为集成测试创建代码覆盖率报告 。 确保从文件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 whichis 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 settingsfor JaCoCo runtime agent.--><propertyName>failsafeArgLine</propertyName></configuration></execution><!--Ensures that the code coverage report for integration tests afterintegration 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 Mavenplugin 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 propertyis true--><skipTests>${skip.integration.tests}</skipTests></configuration></execution></executions>
</plugin>

创建代码覆盖率报告

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

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

  • 开发配置文件在开发过程中使用,它是我们构建的默认配置文件。 当此配置文件处于活动状态时,仅运行单元测试。
  • 集成测试概要文件用于运行集成测试。
  • all-tests配置文件用于运行单元测试和集成测试。

我们可以通过在命令提示符处运行以下命令来创建不同的代码覆盖率报告:

  • 命令mvn clean test运行单元测试,并为目录target / site / jacoco-ut创建单元测试的代码覆盖率报告。
  • 命令mvn clean verify -P integration-test运行集成测试,并创建用于集成测试的代码覆盖率报告到目录target / site / jacoco-it
  • 命令mvn clean verify -P all-tests运行单元测试和集成测试,并为单元测试和集成测试创建代码覆盖率报告。

今天就这些。 与往常一样,此博客文章的示例应用程序可在Github上获得 。

参考: Petri Kainulainen博客上的JCG合作伙伴 Petri Kainulainen 使用JaCoCo Maven插件为单元和集成测试创建代码覆盖率报告 。

翻译自: https://www.javacodegeeks.com/2013/08/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin.html

maven jacoco

maven jacoco_使用JaCoCo Maven插件为单元和集成测试创建代码覆盖率报告相关推荐

  1. 使用JaCoCo Maven插件为单元和集成测试创建代码覆盖率报告

    当我开始使用Java 7时,我立即注意到Cobertura Maven插件不支持它 . 这对我来说是个大问题,因为我每天都使用代码覆盖率报告. 我做了一些研究,发现了JaCoCo代码覆盖库 . 看起来 ...

  2. 系统测试集成测试单元测试_单元和集成测试的代码覆盖率

    系统测试集成测试单元测试 我最近在一个宠物项目中着手构建自动化的UI(集成)测试以及普通的单元测试. 我想将所有这些集成到我的Maven构建中,并提供代码覆盖率报告,以便我可以了解测试覆盖率不足的区域 ...

  3. 单元和集成测试的代码覆盖率

    我最近在一个宠物项目中着手构建自动化的UI(集成)测试以及普通的单元测试. 我想将所有这些集成到我的Maven构建中,并提供代码覆盖率报告,以便我可以了解测试覆盖率不足的区域. 我不仅发布了项目的源代 ...

  4. maven项目使用jacoco插件检测代码覆盖率详细配置

    使用maven构建项目(java项目或者web项目都可以) jacoco插件的配置参考官方网址:http://www.eclemma.org/jacoco/trunk/doc/maven.html ( ...

  5. Maven入门指南⑦:Maven的生命周期和插件

    Maven入门指南⑦:Maven的生命周期和插件 一个完整的项目构建过程通常包括清理.编译.测试.打包.集成测试.验证.部署等步骤,Maven从中抽取了一套完善的.易扩展的生命周期.Maven的生命周 ...

  6. Maven 配置文件 POM 的常用插件配置代码

    Maven 配置文件 POM 的常用插件配置代码 普通 将 Maven 多模块依赖集成打进一个 JAR 包(方法 1) 将 Maven 多模块依赖集成打进一个 JAR 包(方法 2) 生成单入口类 J ...

  7. maven(7)生命周期和插件

    [0]README 1)本文部分文字转自 "maven实战",旨在 review  "maven(7)生命周期和插件" 的相关知识: 2)maven 另外两个核 ...

  8. Maven实战. 3.7NetBeans Maven插件简单使用

    3.7NetBeans Maven插件简单使用 NetBeans的Maven插件也十分简单易用,我们可以轻松地在NetBeans中导入现有的Maven项目,或者使用Archetype创建Maven项目 ...

  9. Maven的生命周期和插件

    (尊重劳动成果,转载请注明出处:https://blog.csdn.net/qq_25827845/article/details/83795622冷血之心的博客) 关注微信公众号(文强的技术小屋), ...

最新文章

  1. HTML frameset 标签
  2. Python中常见的数据类型小结
  3. R语言:常用函数总结
  4. dell mobile connect 兑换码_剑与远征万圣节兑换码是什么?剑与远征2020万圣节兑换码使用解析...
  5. AI大牛发起神秘字母接龙,起因竟然是……
  6. fuck-KUNLUN昆仑ECRS会员管理系统
  7. 服务器安装centos 6.2过程
  8. 【Linux】X window与文本模式的切换
  9. 新兴IT企业特斯拉(六)——Win-Win
  10. 计算机d盘无法格式化,四种方法解决D盘无法格式化问题
  11. kubeadm部署单Master节点kubernetes集群 1.21.0
  12. 科学计算机r系数的操作,R语言实现Fleiss’ Kappa系数处理多个观察者一致性检验...
  13. 百度网盘PC端登录安全验证显示空白页
  14. 文件不能设置默认打开方式怎么办?
  15. 生活与美食,真的如你想象的那么简单吗
  16. TPTP—详细说明讲解
  17. 开发者 J 有意思|1024 开发者嘉年华活动正式启幕
  18. Vue中过滤器和自定义指令详解
  19. android录音波浪动画_Android使用音频信息绘制动态波纹
  20. 行为型模式——备忘录模式(Memento Pattern)

热门文章

  1. Spark SQL(六)之加载数据的参数配置
  2. JavaFX官方教程(五)之在JavaFX中创建表单
  3. equals 和 hashCode 到底有什么联系?一文告诉你
  4. 菜鸟学Java(六)——简单验证码生成(Java版)
  5. 三大框架题目整合考试题(含详解)
  6. RecyclerView多布局的简单使用
  7. win10硬盘修复工具使用教程
  8. shiro-权限概述
  9. HttpServletRequest中getAttribute()和getParameter()的区别
  10. 服务器ubuntu系统版本选型原则,系统集成 - 选择Ubuntu服务器版操作系统的六大理由_服务器应用_Linux公社-Linux系统门户网站...