2

我正在尝试改进我的项目中的代码覆盖率,并希望排除任何View类(因为它们未经测试/可测试)但包含任何ViewModel类......但我似乎无法让通配符过滤器合作,这就是全部或者什么都没有!


def excludedPatterns = [
        //... other stuff

        '**/*Fragment*.*',
        '**/*Activity*.*',
        '**/*Adapter*.*',

        '**/*View*.*', // <-- this line is excluding classes ending w/ ViewModel

        '**/*ViewState*.*',
        '**/*ViewHolder*.*',
]

task codeCoverageReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."

    reports {
        html.enabled true
    }

    def debugTree = fileTree(
            dir: "$project.buildDir/tmp/kotlin-classes/debug",
            excludes: excludedPatterns
    )
    classDirectories.from = files([debugTree])

    def mainSrc = "$project.projectDir/src/main/java"
    sourceDirectories.from = files([mainSrc])

    executionData.from = fileTree(dir: project.buildDir, includes: [
            'jacoco/testDebugUnitTest.exec',
            'outputs/code-coverage/connected/*coverage.ec'
    ])
}

我已经尝试了几种变体**/*View*.*',包括**/*View.*'和其他...有什么我忽略的吗?

4

1 回答 1

4

您可以filter使用fileTree.

例如对于

src/main/java/View.java

class View {}

src/main/java/ViewModel.java

class ViewModel {}

src/main/java/Example.java

class Example {}

src/test/java/ExampleTest.java

import org.junit.Test;

public class ExampleTest {
    @Test
    public final void test() {
    }
}

build.gradle

apply plugin: 'java'
apply plugin: 'jacoco'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'junit:junit:4.13'
}

jacocoTestReport {
    classDirectories.setFrom(
        fileTree(dir: "build/classes/java/main")
            .filter({file -> !file.name.contains('View') || file.name.contains('ViewModel')})
    )
}

执行

gradle clean test jacocoTestReport

build/reports/jacoco/test/html/使用 Gradle 6.2.1 将在不包含View但包含ViewModeland的目录中生成以下报告Example

报告

于 2020-03-04T10:12:01.420 回答