10

我在一个项目中添加了一组没有测试的源,我不希望它们破坏我的测试覆盖率统计数据。我以以下方式配置了 Jacoco:

test {
    jacoco{
        excludes = ['org/bla/**']
        includes = ['com/bla/**']
        append = false
    }
}

jacocoTestReport {
    dependsOn test
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled true
        html.enabled true
    }
    classDirectories = fileTree(dir: 'build/classes/main', include: 'com/bla/**')
    sourceDirectories = fileTree(dir: 'scr/main/java', include: 'com/bla/**')
}

但无论如何,在生成报告时,Jacoco 还包含来自org.bla的类

有人能帮我吗 ?

编辑

经过一些调试,似乎所有默认输出都添加到org.gradle.testing.jacoco.tasks.JacocoReport#classDirectories私有方法中org.gradle.testing.jacoco.plugins.JacocoPlugin#addDefaultReportTasks

使用此类代码时可见:

jacocoTestReport {
    classDirectories = files('build/classes/main/com/bla')
    println("-----------------------------------------------------------")
    getAllClassDirs().each { File file ->
        println(file.absolutePath)
    }
    println("-----------------------------------------------------------")
    getAdditionalClassDirs().each{ File file ->
        println(file.absolutePath)
    }
}

jacocoTestReport << {
    println("-----------------------------------------------------------")
    getAllClassDirs().each { File file ->
        println(file.absolutePath)
    }
    println("-----------------------------------------------------------")
    getAdditionalClassDirs().each{ File file ->
        println(file.absolutePath)
    }
}

Output
-----------------------------------------------------------
<path_here>\build\classes\main\com\bla
-----------------------------------------------------------
....more text here
-----------------------------------------------------------
<path_here>\build\classes\main\com\bla
<path_here>\build\classes\main
<path_here>\build\resources\main
-----------------------------------------------------------

所以 - 问题是:是否可以以某种方式覆盖org.gradle.testing.jacoco.plugins.JacocoPlugin#addDefaultReportTasks方法,或完全覆盖org.gradle.testing.jacoco.plugins.JacocoPlugin类?

4

2 回答 2

11

好的,找到了解决方法:

jacocoTestReport.doFirst{
    classDirectories = files('build/classes/main/com/bla')
}

这将覆盖 JacocoPlugin 类设置的 classDirectories。

于 2013-07-01T18:32:35.443 回答
-1

如果您的任务名称不是

test

运行单元测试,您应该能够生成报告,将以下任务添加到您的 build.gradle 文件中:

jacocoTestReport {
    group = "Reporting"
    description = "Generates Jacoco coverage reports for unit tests"
    executionData = files('build/jacoco/unitTest.exec')
    reports {
        html.destination = 'build/reports/coverage'
    }
}

这需要在运行单元测试的任务之后显式调用,例如:

gradle unitTest jacocoTestReport
于 2013-08-12T20:38:17.250 回答