模块化Spring应用的IT覆盖方案 | AI生成和翻译

Home 2025.09

当前配置总体评估

您当前的配置对于这类小型多模块项目来说非常稳健,特别是已经实现了核心流程:将 JaCoCo 代理附加到 Jetty、通过 Exec 插件运行外部 Python 集成测试,并在 PlaygroundWeb 模块中生成报告。这能捕获通过 Python unittest 测试的 API 端点的运行时覆盖率,正是 Spring Web 应用集成测试(IT)覆盖率所需的关键能力。

核心优势:

已识别的挑战:

您回退到每模块报告的方案很务实,但我们可以优化覆盖率包含机制而避免重复。

推荐策略

专注于在运行应用的模块(此处为 PlaygroundWeb)生成单一综合的 IT 覆盖率报告,同时包含依赖模块(如 PlaygroundUtils)的覆盖率数据。这避免了多次运行测试,并利用了所有代码在单一 JVM 中执行的事实。

为何选择此方案而非聚合?

针对 2 模块项目的分步实施

从小规模开始:在当前设置(1 个应用模块 + 1 个库模块)上应用此方案。测试通过后再扩展。

  1. 保持 IT 执行仅在 PlaygroundWeb
    • 此处无需更改。Jetty 启动 WAR(内嵌 PlaygroundUtils),Python 测试调用端点,覆盖率捕获到 ${project.build.directory}/jacoco.it.exec
    • 确认工具类代码被执行:如果 Python 测试调用了使用 PlaygroundUtils 类(如 SystemUtils)的端点,它们的覆盖率将出现在 .exec 文件中
  2. 增强 PlaygroundWeb 中的 JaCoCo 报告以包含 PlaygroundUtils
    • report 目标中使用 JaCoCo 的 <additionalClassesDirectories><additionalSourceDirectories>。这指示 JaCoCo 针对同一 .exec 文件扫描 PlaygroundUtils 的类/源码
    • 更新 PlaygroundWeb 的 POM(在 jacoco-maven-plugin 配置中):

      <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <executions>
              <!-- 现有的 prepare-agent -->
              <execution>
                  <id>prepare-agent</id>
                  <goals>
                      <goal>prepare-agent</goal>
                  </goals>
              </execution>
              <!-- 增强的报告:包含工具模块 -->
              <execution>
                  <id>report-it</id>
                  <phase>verify</phase>
                  <goals>
                      <goal>report</goal>
                  </goals>
                  <configuration>
                      <dataFile>${jacoco.it.exec}</dataFile>
                      <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
                      <!-- 添加以下内容以包含 PlaygroundUtils 覆盖率 -->
                      <additionalClassesDirectories>
                          <directory>${project.parent.basedir}/PlaygroundUtils/target/classes</directory>
                      </additionalClassesDirectories>
                      <additionalSourceDirectories>
                          <directory>${project.parent.basedir}/PlaygroundUtils/src/main/java</directory>
                      </additionalSourceDirectories>
                  </configuration>
              </execution>
          </executions>
      </plugin>
      
    • 这会生成一个报告(在 PlaygroundWeb/target/site/jacoco-it),覆盖两个模块。您将看到按包/类的细分,包括来自 utils 的 org.lzw
  3. PlaygroundUtils 中禁用/移除 JaCoCo
    • 由于它没有自身的 IT,移除所有 JaCoCo 配置/属性(如 <jacoco.it.exec><it.report.skip>)。它不需要生成自己的报告——覆盖率在上游处理
    • 如果 utils 中有单元测试,保留单独的 prepare-agent + report 用于单元覆盖率(默认 jacoco.exec),但将其与 IT 隔离
  4. 构建与验证
    • 从父项目运行 mvn clean verify
    • Jetty/Python 仅运行一次(在 PlaygroundWeb 中)
    • 检查 PlaygroundWeb/target/site/jacoco-it/index.html:应显示两个模块的类覆盖率
    • 如果 utils 覆盖率为 0%,请确保 Python 测试执行了这些类(例如添加通过端点触发 SystemUtils 的测试)
  5. 可选:强制执行覆盖率阈值
    • PlaygroundWeb 的 JaCoCo 插件中添加 check 执行,如果覆盖率低于阈值(如整体行覆盖率 70%)则使构建失败
      <execution>
          <id>check-it</id>
          <goals>
              <goal>check</goal>
          </goals>
          <configuration>
              <dataFile>${jacoco.it.exec}</dataFile>
              <rules>
                  <rule>
                      <element>BUNDLE</element>
                      <limits>
                          <limit>
                              <counter>LINE</counter>
                              <value>COVEREDRATIO</value>
                              <minimum>0.70</minimum>
                          </limit>
                      </limits>
                  </rule>
              </rules>
          </configuration>
      </execution>
      

扩展到大型项目(如 10+ 模块)

对于 10+ 模块(如多个库 + 1-2 个应用/WAR 模块),扩展上述方案以避免复杂性:

此策略最小化运行次数(每个应用一次 Jetty/Python),清晰处理 JaCoCo 的依赖关系,且能无爆炸性扩展。如果您分享更多细节(如 utils 如何在端点中使用),我可以进一步优化 POM 片段。


Back Donate