Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to aggregate test report for mutli-project build with different plugins?

How can I iterate over that test results of each different type of project and collect it in a single report?

Example project setup:

Root Project
    |
    |- Java Project
    |- test task
    |
    |- Android Library Project (has Build Types)
    |- testDebug task
    |- testRelease task
    |
    |- Android application Project (has Product Flavors and Build Types)
    |- testFreeDebug task
    |- testFreeRelease task
    |- testPaidDebug task
    |- testPaidRelease task

What I have so far:

This will aggregate all test results for all projects:

task aggregateResults(type: Copy) {
    outputs.upToDateWhen { false }
    subprojects { project ->
        from { project*.testResultsDir }
    }
    into { file("$rootDir/$buildDir/results") }
}

task testReport(type: TestReport) {
    outputs.upToDateWhen { false }
    destinationDir = file("$rootDir/$buildDir/reports/allTests")
    subprojects { project ->
        reportOn project.tasks.withType(Test)*.binResultsDir
    }
}

References:

For Java only:

task testReport(type: TestReport) {
    destinationDir = file("$buildDir/reports/allTests")
    reportOn subprojects*.test
}

Source: https://stackoverflow.com/a/16921750/950427

For Android only:

subprojects.each { subproject -> evaluationDependsOn(subproject.name) }

def testTasks = subprojects.collect { it.tasks.withType(Test) }.flatten()

task aggregateResults(type: Copy) {
    from { testTasks*.testResultsDir }
    into { file("$buildDir/results") }
}

Source: https://android.googlesource.com/platform/tools/build/+/nougat-release/build.gradle#79

like image 624
Jared Burrows Avatar asked Jan 27 '17 00:01

Jared Burrows


People also ask

How do I create a test report in Gradle?

How to generate a Test Report. Gradle generates a Test Report automatically when it runs the entire Test Suite. To do the same, run ./gradlew test (or gradlew. bat test from Windows), or run the test Gradle task from your IDE.

Which command lets you generate the unit tests of a project developed with Gradle?

We can run our unit tests by using the command: gradle clean test.


1 Answers

This solution adds specific task to report, only when it's ready. May be applied to heterogeneous/specific tasks as yours.

subprojects {
    // Add custom tasks as part of report when it ready
    gradle.taskGraph.whenReady { graph ->
        if (graph.hasTask(testDebugUnitTest)) {
            rootTestReport.reportOn(testDebugUnitTest)
        }
        // and so on
    }
}

// Combine all 'test' task results into a single HTML report
tasks.register('rootTestReport', TestReport) {
    subprojects.each { dependsOn("${it.name}:testDebugUnitTest") } // todo: to be improved
    destinationDir = file("$buildDir/reports/allTests")
}
like image 112
Ivan Severin Avatar answered Oct 23 '22 12:10

Ivan Severin