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

Jacoco覆盖率报告未生成求助:VSCode Coverage Gutters无法使用

解决Jacoco无法生成覆盖率报告的问题

你的配置存在几处关键问题,调整后即可正常生成覆盖率报告:

问题分析

  • 重复应用Jacoco插件:plugins块已声明id "jacoco",后续又通过apply plugin: 'jacoco'重复加载,属于冗余配置可能引发冲突。
  • 未配置JUnit 5测试支持:依赖使用了JUnit Jupiter,但test任务未指定使用JUnit平台,导致Gradle无法正确识别执行测试,Jacoco无法收集到覆盖数据,最终生成空文件。

修正后的build.gradle配置

plugins {
    id "application"
    id "jacoco"
    id "java" // 统一用plugins块声明Java插件,替代apply方式
}

repositories {
    mavenCentral()
}

ext {
    javaMainClass = "hw6.LegoPaths"
}

application {
    mainClassName = javaMainClass
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
    testImplementation 'org.hamcrest:hamcrest:2.2'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.9.1' // 添加JUnit平台启动器,确保测试正常执行
}

test {
    useJUnitPlatform() // 关键:指定使用JUnit 5平台执行测试
    finalizedBy jacocoTestReport // 测试完成后自动触发报告生成
}

jacocoTestReport {
    reports {
        xml.required = true
        csv.required = true
        html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
    }
}

执行步骤

  1. 清理旧的空文件:./gradlew clean
  2. 重新执行测试并生成报告:./gradlew test jacocoTestReport

执行完成后,你可在build/jacocoHtml目录找到HTML格式报告,build/reports/jacoco/test目录下会生成XML和CSV报告,Coverage Gutters可识别对应文件展示代码覆盖率。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 21:52:24