android - 如何为使用不同插件的多项目构建聚合测试报告?

标签 android gradle android-gradle-plugin android-productflavors test-reporting

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

示例项目设置:

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

我目前拥有的:

这将汇总所有项目的所有测试结果:
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
    }
}

引用:

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

来源: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") }
}

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

最佳答案

此解决方案仅在准备就绪时才将特定任务添加到报告中。可以像您一样应用于异构/特定任务。

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")
}

关于android - 如何为使用不同插件的多项目构建聚合测试报告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41885603/

相关文章:

java - 如果可能,将 List<String> 转换为静态字符串

Android:淡入淡出 View

java - 升级到 Java 11 : -Djava. endorsed.dirs 不支持

android - 找不到 com.android.tools :common:25. 2.3

android - CircleCI 中的单元测试因 org.gradle.internal.exceptions.LocationAwareException 而失败

带字幕的Android自定义弹出菜单

android - UNINITIALIZED VARIABLE 必须初始化变量 _longPressed

java - Mapstruct Annotation Processor 似乎不适用于带有 Gradle 项目的 Intellij

git - 从 Bitbucket 克隆到本地 Android Studio 项目

android - Android Studio卡在新项目上