gradle jacocoTestReport is not working?

2019-04-18 17:17发布

问题:

I have tried to get code coverage in a spring-gradle project using gradle jacoco plugin.

The build.gradle contains the following

apply plugin: "jacoco"

    jacoco {
        toolVersion = "0.7.1.201405082137"
        reportsDir = file("$buildDir/customJacocoReportDir")
    }

    jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}

I then ran

gradle test jacocoTestReport

Where after only the file test.exec is generated in build/reports folder.

Other than that nothing happens.

How can I get the HTML report?

回答1:

Following helped . its in samples/testing/jacaco of gradle-2.3-all.zip

apply plugin: "java"

apply plugin: "jacoco"

jacoco {
    toolVersion = "0.7.1.201405082137"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

repositories {
    mavenCentral()
}

dependencies {
    testCompile "junit:junit:4.+"
}

test {
    jacoco {
        append = false
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        classDumpFile = file("$buildDir/jacoco/classpathdumps")
    }
}


jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}


回答2:

You don't have to configure reportsDir/destinationFile

Because jacoco has default values for them.

build.gradle:

plugins {
    id 'java'
    id 'jacoco'
}

jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled true
        csv.enabled true
    }
}

repositories {
    jcenter()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Run gradle test jacocoTestReport

You can find the test report in ./build/reports/jacoco/test directory.

HTML output is in ./build/reports/jacoco/test/html directory.