如何使用不同的插件聚合mutli-project构建的测试报告?

Jar*_*ows 10 android gradle test-reporting android-gradle-plugin android-productflavors

如何迭代每个不同类型项目的测试结果并将其收集到一个报告中?

示例项目设置:

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
Run Code Online (Sandbox Code Playgroud)

到目前为止我所拥有的:

这将聚合所有项目的所有测试结果:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

参考文献:

仅适用于Java:

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

资料来源:https://stackoverflow.com/a/16921750/950427

仅适用于Android:

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") }
}
Run Code Online (Sandbox Code Playgroud)

资料来源:https://android.googlesource.com/platform/tools/build/+/nougat-release/build.gradle#79

小智 2

此解决方案仅在准备就绪时才添加要报告的特定任务。可以应用于您的异构/特定任务。

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")
}
Run Code Online (Sandbox Code Playgroud)