您可以在支持 Gradle 的 IDE 中打开此示例。

此示例展示了如何聚合多个 Java 子项目的测试结果。test-report-aggregation 插件通过一个独立的实用程序项目来提供此功能,该项目用于指定哪些子项目包含在聚合中。

此示例中的项目包含三个“代码”子项目:applicationlistutilities。所有三个项目都应用了 java 插件,并且 application 通过其 implementation 配置使用 listutilities。第四个子项目 test-results 是用于收集聚合测试结果的独立实用程序项目。

Test Report Aggregation 插件目前不适用于 com.android.application 插件。
test-results/build.gradle.kts
plugins {
    base
    id("test-report-aggregation")
}

dependencies {
    testReportAggregation(project(":application")) (1)
}

reporting {
    reports {
        val testAggregateTestReport by creating(AggregateTestReport::class) { (2)
            testSuiteName = "test"
        }
    }
}

tasks.check {
    dependsOn(tasks.named<TestReport>("testAggregateTestReport")) (3)
}
test-results/build.gradle
plugins {
    id 'base'
    id 'test-report-aggregation'
}

dependencies {
    testReportAggregation project(':application') (1)
}

reporting {
    reports {
        testAggregateTestReport(AggregateTestReport) { (2)
            testSuiteName = "test"
        }
    }
}

tasks.named('check') {
    dependsOn tasks.named('testAggregateTestReport', TestReport) (3)
}

独立项目应用了 test-report-aggregation,但如果 jvm-test-suite 插件也未出现(它将由 java 插件自动应用),则需要额外的配置。

在这种情况下,需要两个额外的设置步骤

1 使用 testReportAggregation 配置声明依赖项
2 定义 AggregateTestReport 类型的报告,该报告从单元测试套件收集测试数据
3 可选:使聚合测试报告生成成为“check”生命周期阶段的一部分

报告聚合逻辑不会自动检查所有子项目的测试结果以进行聚合。相反,将选择 testReportAggregation 配置的直接和传递项目依赖项以进行潜在的聚合。

用户还必须声明一个或多个 AggregateTestReport 类型的报告。每个报告实例指定一个 testType 属性,用于匹配生成测试数据的测试套件。为每个用户定义的报告合成一个 TestReport 任务,并执行聚合。调用此任务将导致在 testReportAggregation 配置的依赖项目中执行测试。

运行测试并生成报告

$ ./gradlew testAggregateTestReport

BUILD SUCCESSFUL
24 actionable tasks: 24 executed

现在可以在 test-results/build/reports/tests/unit-tests/aggregated-results 下找到聚合的 HTML 报告。

有关更多信息,请参阅Java 项目中的测试章节