网站开发组岗位,建设银行官网网站人事,外贸网站建设公司渠道,网络开发语言的有哪些文章目录 概述如何集成pom添加插件Code Demo排除不相关的类CI/CD中使用完整POM 概述
JaCoCo#xff08;Java Code Coverage#xff09;是一个开源的Java代码覆盖率工具#xff0c;它主要用于评估Java程序的测试完整性。通过跟踪测试过程中执行的代码#xff0c;JaCoCo能够… 文章目录 概述如何集成pom添加插件Code Demo排除不相关的类CI/CD中使用完整POM 概述
JaCoCoJava Code Coverage是一个开源的Java代码覆盖率工具它主要用于评估Java程序的测试完整性。通过跟踪测试过程中执行的代码JaCoCo能够提供多种覆盖率指标帮助开发者确保代码的测试质量。这些指标包括指令覆盖、分支覆盖、圈复杂度、行覆盖、方法覆盖和类覆盖。
在实际应用中JaCoCo可以嵌入到构建工具如Maven和Ant中也可以作为Eclipse插件使用。此外它还支持JavaAgent技术能够监控Java程序的执行并收集覆盖率数据。JaCoCo生成的覆盖率报告可以帮助开发者识别未被测试到的代码部分从而指导他们完善测试用例。
JaCoCo的设计旨在提供灵活的集成方式可以与其他开发和测试工具如Sonar和Jenkins集成以增强代码质量和测试流程的管理。它的原理是通过在测试运行时对程序的代码执行情况进行监控并通过一系列的规则和限制来确保代码的测试覆盖程度。这样的工具对于提升软件测试的全面性和深度具有重要作用。 如何集成
集成JaCoCo到你的Java项目中通常涉及以下几个步骤
添加JaCoCo依赖 对于Maven项目你需要在pom.xml文件中添加JaCoCo的依赖。例如dependencies!-- 其他依赖 --dependencygroupIdorg.jacoco/groupIdartifactIdjacoco-maven-plugin/artifactIdversion0.8.3/version !-- 使用最新的版本 --scopetest/scope/dependency
/dependencies对于Gradle项目你需要在build.gradle文件中添加JaCoCo的插件和依赖。例如plugins {id jacoco version 0.8.3 // 使用最新的版本// 其他插件
}配置JaCoCo插件 在pom.xml或build.gradle文件中需要配置JaCoCo插件的行为。这包括设置覆盖率目标、输出报告的格式和路径等。例如在Maven的pom.xml中可能需要配置prepare-agent、report和check等生命周期任务buildpluginsplugingroupIdorg.jacoco/groupIdartifactIdjacoco-maven-plugin/artifactIdversion0.8.3/versionexecutionsexecutionidprepare-agent/idgoalsgoalprepare-agent/goal/goals/executionexecutionidreport/idphasetest/phasegoalsgoalreport/goal/goals/execution!-- 强制要求覆盖率 --executionidcheck-code-coverage/idphasetest/phasegoalsgoalcheck/goal/goalsconfigurationrulesruleelementBUNDLE/elementlimitslimitcounterINSTRUCTION/countervalueCOVEREDRATIO/valueminimum0.80/minimum !-- 至少80%的代码被执行 --/limit!-- 可以添加更多的规则 --/limits/rule/rules/configuration/execution/executions/plugin/plugins
/build运行测试并生成覆盖率报告 使用Maven的mvn test命令或者Gradle的gradle test命令运行你的测试。测试完成后JaCoCo会生成覆盖率报告通常在target/site/jacoco目录下对于Maven项目。 分析覆盖率报告 打开生成的HTML报告分析覆盖率数据。识别未覆盖到的代码区域并补充相应的测试用例。 集成到持续集成/持续部署CI/CD流程可选 将JaCoCo集成到你的CI/CD工具链中比如Jenkins、Travis CI、GitLab CI等。在CI/CD配置中添加步骤来运行测试并生成覆盖率报告。 使用JaCoCo的命令行工具可选 使用JaCoCo提供的命令行工具来生成报告如jacoco coverage report。可以配置命令行工具来与IDE或构建工具集成。
请注意具体的集成步骤和配置可能会根据所使用的构建工具、IDE和项目设置有所不同。因此建议查阅最新的JaCoCo官方文档 。 接下来我们以以Spring Boot 为例 看看如何完成集成
pom添加插件
pom.xml中增加如下配置
buildpluginManagementpluginsplugingroupIdorg.jacoco/groupIdartifactIdjacoco-maven-plugin/artifactIdversion0.8.8/version/plugin/plugins/pluginManagementpluginsplugingroupIdorg.jacoco/groupIdartifactIdjacoco-maven-plugin/artifactIdexecutionsexecutiongoalsgoalprepare-agent/goal/goals/executionexecutionidreport/idphasetest/phasegoalsgoalreport/goal/goals/execution/executions/plugin/plugins
/buildhttps://www.jacoco.org/jacoco/trunk/doc/maven.html Code Demo package com.artisan.service;/*** author 小工匠* version 1.0* mark: show me the code , change the world*/
public class ShippingService {public int calculateShippingFee(int weight) {if (weight 0) {throw new IllegalStateException(Please provide correct weight);}if (weight 2) {return 5;} else if (weight 5) {return 10;}return 15;}
}
package com.artisan.service;import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;/*** author 小工匠* version 1.0 * mark: show me the code , change the world*/
public class TestShippingService {Testpublic void incorrectWeight() {ShippingService shippingService new ShippingService();assertThrows(IllegalStateException.class, () - shippingService.calculateShippingFee(-1));}Testpublic void firstRangeWeight() {ShippingService shippingService new ShippingService();assertEquals(5, shippingService.calculateShippingFee(1));}
}
转到 Maven选择 clean 和 test 命令然后选择 Run Maven Build 测试完成后 target/site/jacoco/index.html 包含所有输出。 在图像中看到boot-jarcoo是项目名称com.artisan.service 是包。显示代码已覆盖 68%分支已覆盖 50%。
点击com.artisan.service 进入详情 ShippingService 里面代码已经覆盖了 68%分支已经覆盖了 50% 。
进入 ShippingService 类 打开 calculateShippingfee(int) 方法 Jacoco 在这里非常清楚地展示了不同级别的覆盖范围。它使用不同颜色的菱形图标来表示分支的代码覆盖率。并使用背景颜色来表示行的代码覆盖率。
绿色菱形表示所有分支均已被覆盖。黄色菱形意味着代码已被部分覆盖 一些未经测试的分支。红色菱形表示测试期间没有使用任何分支。 接下来添加更多代码来覆盖部分覆盖的分支。 Testpublic void secondRangeWeight() {ShippingService shippingService new ShippingService();assertEquals(10, shippingService.calculateShippingFee(4));}Run Maven Build 再次使用 clean 和 test 命令再次在浏览器中打开 calculateShippingfee(int) 方法的测试覆盖率。 可以看到黄色钻石仍然在那里。这意味着我们还没有涵盖权重大于 5 的场景。让我们再添加一个测试用例 Testpublic void lastRangeWeight() {ShippingService shippingService new ShippingService();assertEquals(15, shippingService.calculateShippingFee(10));}可以看到所有的场景都已经被完全覆盖了。 排除不相关的类 意到 App类对于覆盖率报告并不是非常重要。在某些情况下此类的覆盖率可能会扭曲整体代码覆盖率报告。为了避免此类不相关的类影响代码覆盖率我们可以使用Jacoco插件将其排除。
pluginsplugingroupIdorg.jacoco/groupIdartifactIdjacoco-maven-plugin/artifactIdconfigurationexcludesexcludecom/artisan/App.class/exclude/excludes/configuration.../plugin
/pluginshttps://www.eclemma.org/jacoco/trunk/doc/report-mojo.html#excludes
重新编译测试得到报告 CI/CD中使用
现在假设我们使用 CI/CD 来部署代码我们可能想验证已经完成了多少行代码覆盖率或代码覆盖率百分比等。为此我们需要在Jacoco 插件配置
executionidjacoco-check/idgoalsgoalcheck/goal/goalsconfigurationrulesruleelementPACKAGE/elementlimitslimitcounterLINE/countervalueCOVEREDRATIO/valueminimum90%/minimum/limit/limits/rule/rules/configuration
/execution在此执行中我们添加了一条规则。规则是对于 PACKAGE计数应为 LINE并且 LINE 覆盖率最小应为 90%.
转到 Maven选择 clean 和 verify 命令然后选择 Run Maven Build 进行检查。 为了验证这个功能我们先去掉 Testpublic void secondRangeWeight() {ShippingService shippingService new ShippingService();assertEquals(10, shippingService.calculateShippingFee(4));}Testpublic void lastRangeWeight() {ShippingService shippingService new ShippingService();assertEquals(15, shippingService.calculateShippingFee(10));}再 选择 clean 和 verify 命令然后选择 Run Maven Build 进行检查。
可以看到它失败了。原因清楚地表明违反了规则“线路覆盖率为0.62但预期最小值为0.90”。
现在让我们更新 LINE 覆盖率最小值为 60%然后再次运行。 完整POM
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdboot2/artifactIdgroupIdcom.artisan/groupIdversion0.0.1-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdboot-jarcoo/artifactIdpackagingjar/packagingnameboot-jarcoo/nameurlhttp://maven.apache.org/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/versionscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesbuildpluginManagementpluginsplugingroupIdorg.jacoco/groupIdartifactIdjacoco-maven-plugin/artifactIdversion0.8.8/version/plugin/plugins/pluginManagementpluginsplugingroupIdorg.jacoco/groupIdartifactIdjacoco-maven-plugin/artifactIdconfigurationexcludesexcludecom/artisan/App.class/exclude/excludes/configurationexecutionsexecutiongoalsgoalprepare-agent/goal/goals/executionexecutionidjacoco-check/idgoalsgoalcheck/goal/goalsconfigurationrulesruleelementPACKAGE/elementlimitslimitcounterLINE/countervalueCOVEREDRATIO/valueminimum60%/minimum/limit/limits/rule/rules/configuration/executionexecutionidreport/idphasetest/phasegoalsgoalreport/goal/goals/execution/executions/plugin/plugins/build
/project