0


【软将军-jacoco】执行jacoco进行单元测试,pom里应该配置什么依赖和插件,pom里面配置起的作用是什么?

执行jacoco进行单元测试,pom里应该配置什么依赖和插件

一、

要在 Maven 项目中使用 JaCoCo 进行单元测试,您需要在

  1. pom.xml

文件中添加 JaCoCo 的依赖和插件。这样配置后,您可以收集覆盖率数据并生成相应的报告。以下是详细的配置示例及其作用说明。

1. 添加 JaCoCo 依赖和插件

  1. pom.xml

中,您需要在

  1. dependencies

  1. build/plugins

部分添加 JaCoCo 的相关配置。

示例 POM 配置
  1. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>your-project</artifactId><version>1.0-SNAPSHOT</version><properties><jacoco.version>0.8.8</jacoco.version><!-- JaCoCo 版本 --></properties><dependencies><!-- JUnit 依赖,用于单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!-- 其他依赖项 --></dependencies><build><plugins><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>${jacoco.version}</version><executions><execution><goals><goal>prepare-agent</goal><!-- 为测试执行准备代理 --></goals></execution><execution><id>report</id><!-- 生成报告的执行 --><phase>test</phase><!-- 在测试阶段之后执行 --><goals><goal>report</goal></goals></execution></executions></plugin><!-- 其他插件 --></plugins></build></project>

2. 关键配置项说明

  • JaCoCo 依赖:- jacoco-maven-plugin:用于在构建过程中集成 JaCoCo,收集代码覆盖率数据并生成报告。
  • 执行目标设置:- prepare-agent:在执行测试之前,会进行配置,以便 JaCoCo 可以收集执行过程中的覆盖率信息。这会在测试阶段启动 JVM 时注入 JaCoCo 代理。
  • 报告生成:- report:在测试执行后,它将生成覆盖率报告。您可以在执行 mvn test 时自动生成 HTML 格式的代码覆盖率报告,报告通常位于 target/site/jacoco 目录下。

3. 运行单元测试并生成报告

配置完

  1. pom.xml

后,运行以下命令执行单元测试并生成覆盖率报告:

  1. mvn clean test

您可以在

  1. target/site/jacoco/index.html

中查看生成的覆盖率报告,它将展示各个类和方法的执行覆盖率情况。

总结

配置 JaCoCo 在 Maven 项目中进行单元测试的作用主要包括:

  • 提供代码覆盖率的实时收集功能,帮助开发者了解未测试的代码部分。
  • 自动生成详细的覆盖率报告,以便于检查代码质量和提高测试的全面性。
  • 可以集成到 CI/CD 流程中,确保在代码合并或发布之前满足特定的覆盖率标准。

二、

在 Maven 项目中使用 JaCoCo 进行单元测试覆盖率分析,需要在

  1. pom.xml

文件中配置 JaCoCo 的插件和相关依赖。以下是如何配置 JaCoCo 的详细说明,以及这些配置在

  1. pom.xml

中的作用。

配置 JaCoCo 插件

  1. pom.xml

中,主要需要配置 JaCoCo Maven 插件。这个插件负责在测试执行时收集代码覆盖率数据,并生成报告。

  1. <build><plugins><!-- JaCoCo Maven Plugin --><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.8</version><!-- 请使用最新稳定版本 --><executions><!-- 绑定到测试阶段 --><execution><goals><goal>prepare-agent</goal></goals></execution><!-- 绑定到报告生成阶段 --><execution><id>report</id><phase>verify</phase><goals><goal>report</goal></goals></execution></executions></plugin></plugins></build>

配置说明

  1. **<plugin>**:定义了 JaCoCo Maven 插件的基本信息,包括 groupIdartifactIdversion
  2. **<executions>**:指定插件的执行阶段和目标。- **<execution>**:定义插件的一个执行配置。 - **<goals>**:指定要执行的目标。 - **prepare-agent**:在测试阶段之前准备 JaCoCo 代理,以便在测试运行时收集覆盖率数据。- **report**:在 verify 阶段生成覆盖率报告。
  3. **<phase>**:指定插件目标绑定的生命周期阶段。- **verify**:在测试完成后生成覆盖率报告。

作用

  • 准备 JaCoCo 代理prepare-agent 目标在测试运行之前启动 JaCoCo 代理,负责在测试执行期间收集覆盖率数据。
  • 生成覆盖率报告report 目标在 verify 阶段生成覆盖率报告,通常以 HTML 格式输出,便于开发者查看哪些代码被测试覆盖。

使用场景

  • 单元测试覆盖率分析:通过在 pom.xml 中配置 JaCoCo 插件,开发者可以在每次构建时自动收集和生成代码覆盖率报告,帮助识别未被测试覆盖的代码区域。
  • 持续集成:在 CI/CD 环境中,JaCoCo 插件可以自动生成覆盖率报告,确保代码质量和测试覆盖率符合项目要求。

运行命令

配置完成后,可以使用以下命令运行测试并生成覆盖率报告:

  1. mvn clean test

这将执行项目的单元测试,并在

  1. target/site/jacoco

目录中生成覆盖率报告。通过这种方式,开发者可以轻松地在项目中集成 JaCoCo 以进行代码覆盖率分析。

三、

在使用

  1. jacoco-maven-plugin

时,

  1. executions

部分的配置可以根据项目需求进行调整,但通常至少需要以下两个执行配置,以确保能够正确收集和生成代码覆盖率报告:

必要的执行配置

  1. **prepare-agent**:- 目的:在测试运行之前,准备 JaCoCo 代理,以便在测试执行期间收集覆盖率数据。- 绑定阶段:通常绑定到 initializetest 阶段。<execution><goals><goal>prepare-agent</goal></goals></execution>
  2. **report**:- 目的:在测试完成后,生成覆盖率报告。- 绑定阶段:通常绑定到 verify 阶段,以确保在所有测试执行完毕后生成报告。<execution><id>report</id><phase>verify</phase><goals><goal>report</goal></goals></execution>

可选的执行配置

根据项目的具体需求,您还可以添加以下可选的执行配置:

  1. **check**:- 目的:在构建过程中检查代码覆盖率是否达到预期的阈值。- 绑定阶段:通常绑定到 verify 阶段。- 配置:可以通过 <configuration> 元素设置覆盖率阈值。<execution><id>check</id><goals><goal>check</goal></goals><configuration><rules><rule><element>BUNDLE</element><limits><limit><counter>INSTRUCTION</counter><value>COVEREDRATIO</value><minimum>0.80</minimum><!-- 80% 覆盖率 --></limit></limits></rule></rules></configuration></execution>

完整示例

以下是一个完整的

  1. jacoco-maven-plugin

配置示例,包含必要的执行配置:

  1. <build><plugins><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.8</version><executions><execution><goals><goal>prepare-agent</goal></goals></execution><execution><id>report</id><phase>verify</phase><goals><goal>report</goal></goals></execution><execution><id>check</id><phase>verify</phase><goals><goal>check</goal></goals><configuration><rules><rule><element>BUNDLE</element><limits><limit><counter>INSTRUCTION</counter><value>COVEREDRATIO</value><minimum>0.80</minimum></limit></limits></rule></rules></configuration></execution></executions></plugin></plugins></build>

总结

  • prepare-agent 是必需的,以便在测试运行时收集覆盖率数据。
  • report 是必需的,以便在测试完成后生成覆盖率报告。
  • check 是可选的,用于在构建过程中验证覆盖率是否达到预期标准。

通过这些配置,您可以确保在构建过程中正确收集和报告代码覆盖率信息。

四、

除了基本的

  1. prepare-agent

  1. report

配置外,

  1. jacoco-maven-plugin

还提供了一些其他有用的执行配置和功能,可以根据项目的具体需求进行配置。以下是一些额外的配置选项和它们的作用:

其他配置选项

  1. dump(转储覆盖率数据)- 目的:在运行时从 JVM 中提取覆盖率数据。- 使用场景:当需要在应用程序运行时动态收集覆盖率数据,而不是通过测试框架时。<execution><id>dump</id><phase>post-integration-test</phase><goals><goal>dump</goal></goals><configuration><address>localhost</address><port>6300</port><destFile>${project.build.directory}/jacoco-it.exec</destFile><append>true</append></configuration></execution>
  2. instrument(字节码插桩)- 目的:在构建过程中对字节码进行插桩,以便在运行时收集覆盖率数据。- 使用场景:当需要在不修改源代码的情况下收集覆盖率数据时。<execution><id>instrument</id><phase>process-classes</phase><goals><goal>instrument</goal></goals><configuration><destFile>${project.build.directory}/instrumented-classes</destFile></configuration></execution>
  3. restore-instrumented-classes(恢复原始字节码)- 目的:在插桩后恢复原始字节码。- 使用场景:在插桩后需要恢复原始字节码以进行其他构建步骤时。<execution><id>restore</id><phase>process-classes</phase><goals><goal>restore-instrumented-classes</goal></goals></execution>

配置示例

以下是一个包含多个执行配置的完整示例:

  1. <build><plugins><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.8</version><executions><execution><goals><goal>prepare-agent</goal></goals></execution><execution><id>report</id><phase>verify</phase><goals><goal>report</goal></goals></execution><execution><id>check</id><phase>verify</phase><goals><goal>check</goal></goals><configuration><rules><rule><element>BUNDLE</element><limits><limit><counter>INSTRUCTION</counter><value>COVEREDRATIO</value><minimum>0.80</minimum></limit></limits></rule></rules></configuration></execution><execution><id>dump</id><phase>post-integration-test</phase><goals><goal>dump</goal></goals><configuration><address>localhost</address><port>6300</port><destFile>${project.build.directory}/jacoco-it.exec</destFile><append>true</append></configuration></execution></executions></plugin></plugins></build>

总结

  • 基本配置prepare-agentreport 是最常用的配置,用于收集和生成覆盖率报告。
  • 高级配置dumpinstrumentrestore-instrumented-classes 等配置提供了更高级的功能,适用于特定的需求,如动态数据收集和字节码插桩。
  • 灵活性:根据项目的具体需求,可以灵活地选择和组合这些配置,以实现最佳的覆盖率分析和报告生成。

五、

在使用

  1. jacoco-maven-plugin

时,

  1. instrument

  1. restore-instrumented-classes

这两个执行配置并不是必须的。它们提供了高级功能,主要用于特定的场景。以下是它们的详细说明和使用场景:

  1. instrument

  1. restore-instrumented-classes

的作用

  1. instrument- 目的:对字节码进行插桩,以便在运行时收集覆盖率数据。- 使用场景:当你需要在不通过测试框架(如 JUnit 或 TestNG)运行的情况下收集覆盖率数据时,或者在某些特殊的构建流程中需要对字节码进行插桩。
  2. restore-instrumented-classes- 目的:在插桩后恢复原始字节码。- 使用场景:在插桩后需要恢复原始字节码以进行其他构建步骤时,确保后续的构建过程不受插桩影响。

是否必须?

  • 不是必须的:对于大多数标准的单元测试覆盖率收集和报告生成场景,prepare-agentreport 是足够的。这两个配置已经可以满足通过测试框架运行测试并生成覆盖率报告的需求。
  • 特定需求instrumentrestore-instrumented-classes 主要用于高级场景,比如需要在运行时动态收集覆盖率数据,或者在某些复杂的构建流程中需要对字节码进行特殊处理。

典型使用场景

  • 动态应用程序监控:在运行中的应用程序中收集覆盖率数据,而不是通过测试框架。
  • 复杂构建流程:在构建过程中需要对字节码进行插桩以进行特定的分析或处理。

总结

对于大多数项目,尤其是那些通过标准测试框架进行单元测试的项目,

  1. instrument

  1. restore-instrumented-classes

并不是必需的。它们提供了额外的灵活性和功能,适用于需要更复杂覆盖率分析的场景。如果你的项目没有这些特殊需求,可以忽略这两个配置。

六、

  1. jacoco-maven-plugin

  1. maven-surefire-plugin

是两个不同的 Maven 插件,它们在项目构建和测试过程中扮演不同的角色。以下是它们的区别和各自的作用:

jacoco-maven-plugin

  • 主要功能:用于收集和报告代码覆盖率。
  • 作用: - 在测试执行期间收集代码覆盖率数据。- 生成覆盖率报告(如 HTML、XML 格式)。- 可选地检查覆盖率是否达到预设标准。
  • 使用场景:当你需要分析代码覆盖率以确保测试的全面性和代码质量时使用。

maven-surefire-plugin

  • 主要功能:用于运行单元测试。
  • 作用: - 执行项目中的单元测试(通常是 JUnit 或 TestNG 测试)。- 提供测试结果报告。- 支持并行测试、测试过滤等高级功能。
  • 使用场景:在构建过程中执行单元测试是必需的,因此 maven-surefire-plugin 是大多数 Maven 项目中必不可少的插件。

是否必须使用 maven-surefire-plugin?

  • 通常是必须的:如果你的项目需要运行单元测试,那么 maven-surefire-plugin 是必需的,因为它是 Maven 默认用于执行单元测试的插件。
  • 替代方案:在某些情况下,你可能会使用 maven-failsafe-plugin 来运行集成测试,但这通常是与 maven-surefire-plugin 结合使用的,而不是替代。

结合使用

  • 协同工作jacoco-maven-plugin 通常与 maven-surefire-plugin 一起使用。maven-surefire-plugin 负责运行测试,而 jacoco-maven-plugin 在测试运行时收集覆盖率数据。
  • 配置示例<build><plugins><!-- Surefire Plugin for running tests --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M5</version></plugin><!-- JaCoCo Plugin for coverage analysis --><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.8</version><executions><execution><goals><goal>prepare-agent</goal></goals></execution><execution><id>report</id><phase>verify</phase><goals><goal>report</goal></goals></execution></executions></plugin></plugins></build>

总结

  • maven-surefire-plugin 是用于运行单元测试的插件,通常是必需的。
  • jacoco-maven-plugin 是用于收集和报告代码覆盖率的插件,通常与 maven-surefire-plugin 一起使用。
  • 两者结合使用可以确保在运行测试的同时收集覆盖率数据,从而帮助开发者提高代码质量和测试覆盖率。

本文转载自: https://blog.csdn.net/weixin_46453070/article/details/142658947
版权归原作者 阿寻寻 所有, 如有侵权,请联系我们删除。

“【软将军-jacoco】执行jacoco进行单元测试,pom里应该配置什么依赖和插件,pom里面配置起的作用是什么?”的评论:

还没有评论