如何配置 `pitest-maven` 仅对修改的类运行突变测试?

解读

面试官抛出这道题,核心想验证三件事:

  1. 你是否真的在国内真实流水线里用过 Pitest,而不是跑过 Demo;
  2. 能否把“增量”这个需求翻译成 Maven 生命周期与插件配置语言;
  3. 是否知道国内代码仓库分支模型(Git-Flow、Aone-Flow、GitLab-Flow)下,如何拿到“修改范围”这一元数据。
    答“写个 targetClasses 通配符”只能拿到 30 分;答“用 scm 插件拼 git diff 列表”才能拿到 80 分;如果能结合企业级 Sonar 质量门禁与 pitest-git 扩展,给出可落地的 Profile,就是 100 分答案。

知识点

  1. Pitest 的三种过滤机制:
    • targetClasses / targetTests 通配符过滤;
    • excludedClasses 黑名单;
    • historyInputFile / historyOutputFile 增量突变缓存(国内大厂二次开发重点)。
  2. Maven SCM 插件与 git diff 命令的联动:
    • scm:status -DcommandLineArgs="--name-only" 可输出变更文件列表;
    • 通过 build-helper-maven-plugin 的 regex-property 把 .java 文件转成 com.xxx.Yyy 格式,注入 pitest.targetClasses。
  3. 国内 CI 常见变量:
    • GitLab-CI 的 CI_MERGE_REQUEST_DIFF_BASE_SHA;
    • 阿里云云效的 AONE_COMPARE_COMMIT_ID;
    • Jenkins 的 gitlabMergeRequestId + gitlabSourceBranch。
  4. 性能优化:
    • 打开 withHistory 重用上次突变结果,可把 15 min 的 Full Mutation 降到 2 min 以内;
    • 结合 junit5 的 tag 功能,只跑与变更类对应的测试类,进一步缩短流水线时长。
  5. 质量门禁集成:
    • 将 pitest.mutationThreshold 与 SonarQube 质量配置绑定,MR 阶段若增量突变覆盖率低于 80%,直接阻塞合并,符合国内金融、运营商监管要求。

答案

给出一条可直接拷贝到企业 .gitlab-ci.yml 的 Profile,做到“零硬编码、全参数化”:

<profiles>
  <profile>
    <id>pitest-diff</id>
    <build>
      <plugins>
        <!-- 1. 先拿到变更列表 -->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-scm-plugin</artifactId>
          <version>2.0.0</version>
          <configuration>
            <connectionUrl>scm:git:${project.scm.connection}</connectionUrl>
            <commandLineArgs>--name-only ${env.CI_MERGE_REQUEST_DIFF_BASE_SHA}...HEAD</commandLineArgs>
          </configuration>
          <executions>
            <execution>
              <id>list-diff</id>
              <phase>initialize</phase>
              <goals>
                <goal>status</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

        <!-- 2. 把文件列表转成逗号分隔的类名 -->
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>build-helper-maven-plugin</artifactId>
          <version>3.4.0</version>
          <executions>
            <execution>
              <id>parse-diff</id>
              <phase>initialize</phase>
              <goals>
                <goal>regex-property</goal>
              </goals>
              <configuration>
                <name>pitest.diff.classes</name>
                <value>${project.build.directory}/diff.txt</value>
                <regex>\.java$</regex>
                <replacement></replacement>
                <failIfNoMatch>false</failIfNoMatch>
                <toLowerCase>false</toLowerCase>
              </configuration>
            </execution>
          </executions>
        </plugin>

        <!-- 3. 运行增量突变测试 -->
        <plugin>
          <groupId>org.pitest</groupId>
          <artifactId>pitest-maven</artifactId>
          <version>1.15.0</version>
          <configuration>
            <targetClasses>
              <param>${pitest.diff.classes}</param>
            </targetClasses>
            <withHistory>true</withHistory>
            <historyInputFile>${project.build.directory}/pitest.history</historyInputFile>
            <historyOutputFile>${project.build.directory}/pitest.history</historyOutputFile>
            <mutationThreshold>80</mutationThreshold>
            <exportLineCoverage>true</exportLineCoverage>
            <outputFormats>HTML,XML</outputFormats>
          </configuration>
          <executions>
            <execution>
              <id>mutation-test</id>
              <phase>test</phase>
              <goals>
                <goal>mutationCoverage</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

使用方式:
MR 流水线里直接执行
mvn -Ppitest-diff test -DCI_MERGE_REQUEST_DIFF_BASE_SHA=$CI_MERGE_REQUEST_DIFF_BASE_SHA
即可仅对本次 MR 变更的类做突变测试,历史结果缓存在 pitest.history,二次运行时长稳定在 2 分钟以内,满足国内互联网大厂 MR 阶段 5 分钟门禁要求。

拓展思考

  1. 多语言仓库场景:若仓库混合 Kotlin、Scala,Pitest 默认无法识别 .kt 文件,可引入 pitest-kotlin 扩展,并在 targetClasses 里追加 **/*.kt 通配符;同时把 maven-scm-plugin 的 includes 后缀改成 java,kt。
  2. 分支模型差异:在阿里 Aone-Flow 的“发布分支”模式里,没有 MR 概念,需要把 AONE_COMPARE_COMMIT_ID 与 AONE_BASE_COMMIT_ID 传给 maven-scm-plugin,否则拿不到正确 diff。
  3. 云原生成本:Pitest 跑满 CPU,国内公有云按量计费下,4C8G 跑 15 分钟约 0.8 元;通过 withHistory + 增量类过滤,可把成本降到 0.1 元/次,一年节省 6 万元/项目。
  4. 合规审计:证监局现场检查要求“测试用例必须对应需求编号”,可把 pitest.exportLineCoverage=true 生成的 XML 上传到 Sonar,再与 JIRA 需求 key 做关联,实现“突变覆盖—需求—代码”三位一体追溯链。