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

如何配置Jacoco插件读取自定义Gradle测试任务的输出?

解决自定义Gradle测试任务的Jacoco报告不更新问题

默认的jacocoTestReport任务只会收集默认test任务生成的覆盖率执行数据,你自定义的testAll任务的测试数据没被纳入报告范围,所以运行完testAll再执行报告任务,结果还是旧数据。

解决方案一:让默认报告包含自定义任务数据

修改build.gradle,给jacocoTestReport添加自定义任务的执行数据:

plugins {
    id 'java'
    id 'jacoco'
}

tasks.register('testAll', Test) {
    environment "FILTER", "ALL"           
    useJUnitPlatform()
}

jacocoTestReport {
    // 同时收集默认test和自定义testAll的执行数据
    executionData tasks.test, tasks.testAll
    // 可选:执行报告前自动运行testAll任务
    dependsOn testAll
}

执行./gradlew jacocoTestReport,报告会同时包含两个测试任务的覆盖率数据。

解决方案二:单独为自定义任务创建报告

如果只想生成testAll任务的专属覆盖率报告,可以单独注册一个Jacoco报告任务:

plugins {
    id 'java'
    id 'jacoco'
}

tasks.register('testAll', Test) {
    environment "FILTER", "ALL"           
    useJUnitPlatform()
}

tasks.register('jacocoTestAllReport', JacocoReport) {
    // 仅收集testAll的执行数据
    executionData tasks.testAll
    // 指定要分析的源码集
    sourceSets sourceSets.main
    // 配置报告格式(默认已开启HTML)
    reports {
        html.required = true
        xml.required = false
        csv.required = false
    }
}

// 执行报告任务前自动运行testAll
jacocoTestAllReport.dependsOn testAll

执行./gradlew jacocoTestAllReport,会在build/reports/jacoco/jacocoTestAllReport/html下生成专属报告。

内容的提问来源于stack exchange,提问作者MiguelSlv

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 12:13:17