You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Gradle项目中Jacoco的jacocoTestReport.xml包含自定义sourceSet覆盖率

问题原因

你当前仅为Jacoco报告指定了执行数据文件,未明确告知Jacoco要统计覆盖率的源码和编译类路径,导致无法将exec文件的执行记录和业务代码关联,所以报告没有对应覆盖率数据。

调整配置

在原有配置基础上补充以下内容即可:

// 显式将Jacoco插件绑定到自定义的componentTest测试任务,确保执行测试时正常生成componentTest.exec
jacoco {
    applyTo componentTest
}

jacocoTestReport {
    executionData(
            file("${project.buildDir}/jacoco/test.exec"),
            file("${project.buildDir}/jacoco/componentTest.exec"),
    )
    // 指定需要统计覆盖率的业务代码源码集合
    sourceSets sourceSets.main
    // 指定需要统计覆盖率的编译后类文件路径,过滤无效目录,排除测试类本身
    classDirectories.setFrom(
        files(sourceSets.main.output.classesDirs)
            .filter { it.exists() }
    )
}

// 配置报告任务依赖所有测试任务,确保执行报告前所有exec文件都已生成
jacocoTestReport.dependsOn test, componentTest

验证方法

  • 执行构建命令:./gradlew clean check jacocoTestReport(Windows环境替换为gradlew.bat clean check jacocoTestReport)
  • 检查build/jacoco目录下是否存在非空的test.exec和componentTest.exec文件
  • 查看生成的build/reports/jacoco/test/jacocoTestReport.xml文件,确认已经包含全量覆盖率数据

内容的提问来源于stack exchange,提问作者Lukasz G.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 08:30:02