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

多模块Gradle项目Jacoco报告含测试类问题及修复咨询

Fix Jacoco Report Including Test Package Classes

Hey, I see the issue here—your current Jacoco configuration isn't filtering out compiled test classes, which is why your coverage report is including test package code and skewing the results. We only want to measure coverage for your main source code, right?

Here's the corrected Gradle configuration for your multi-module project:

subprojects { subproject ->
    jacocoTestReport {
        additionalSourceDirs = files(sourceSets.main.allSource.srcDirs)
        
        // 🔑 Critical fix: Filter class directories to only include main source code
        classDirectories.setFrom(
            files(sourceSets.main.output).filter { file ->
                file.path.startsWith(sourceSets.main.java.outputDir.path)
            }
        )
        
        sourceDirectories = files(sourceSets.main.allSource.srcDirs)
        reports {
            html.enabled = true
            xml.enabled = true
            csv.enabled = false
        }
    }

    test {
        useTestNG()
    }

    test.finalizedBy(project.tasks.jacocoTestReport)
}

What's changed?

The key adjustment is the classDirectories configuration:

  • We explicitly target only the compiled classes from your main source set using sourceSets.main.output.
  • The filter ensures we only include files from the main code's output directory, excluding any test code compilation artifacts that might have been included by default.

If you want even more precision (e.g., targeting specific packages in your main code), you can use path matching instead:

classDirectories.setFrom(
    sourceSets.main.output.asFileTree.matching {
        include 'com/yourcompany/**' // Replace with your main package path
        exclude 'test/**' // Explicitly exclude test-related classes just to be safe
    }
)

After applying this change, your Jacoco report will only show coverage for code in the main package, giving you accurate, meaningful coverage metrics.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:27:54