测试报告聚合插件(插件 ID:test-report-aggregation)提供了用于将多个 Test 任务调用(可能跨越多个 Gradle 项目)的结果聚合到一个 HTML 报告中的任务和配置。

用法

要使用测试报告聚合插件,请在您的构建脚本中包含以下内容

plugins {
    id 'test-report-aggregation'
}
plugins {
    id("test-report-aggregation")
}

请注意,除非与 JVM 测试套件插件结合使用,否则此插件不会执行任何操作。Java 插件会自动应用 JVM 测试套件插件。

现在有两种方法可以在多个子项目中收集测试结果

  1. 从分发项目的子项目,例如应用程序或 WAR 子项目 → 分发示例

  2. 使用独立项目指定子项目 → 独立示例

示例 2 也可以用于通过根项目聚合结果。

测试报告聚合插件目前不适用于 com.android.application 插件。

任务

当项目也应用了 jvm-test-suite 插件时,会为每个测试套件添加以下任务

testSuiteAggregateTestReportTestReport

依赖于:匹配以下属性的变体的工件

通过 testReportAggregation 配置收集直接和传递项目依赖项的变体。将匹配以下属性

    - org.gradle.category         = verification   (1)
    - org.gradle.testsuite.name   = test           (2)
    - org.gradle.verificationtype = test-results   (3)
1 Category 属性;值是固定的。
2 TestSuiteName 属性;值派生自 TestSuite#getName()
3 VerificationType 属性;值是固定的。

有关测试执行生成的变体的更多信息,请参阅 JVM 测试套件插件文档的输出变体部分。

报告

默认情况下,当任何任务失败(包括测试失败)时,Gradle 会停止执行任务。为了确保您的构建始终生成聚合报告,请在 Gradle 命令中指定 --continue 选项。有关更多信息,请参阅失败时继续构建

自动报告创建

当项目也应用了 jvm-test-suite 插件时,会为每个测试套件添加以下报告对象

testSuiteAggregateTestReportAggregateTestReport

创建一个聚合 Jacoco 报告,聚合所有项目依赖项中具有给定名称的所有测试套件。

手动报告创建

当项目不应用 jvm-test-suite 插件时,您必须手动注册一个或多个报告

build.gradle.kts
reporting {
    reports {
        val testAggregateTestReport by creating(AggregateTestReport::class) { (1)
            testSuiteName = "test"
        }
    }
}
build.gradle
reporting {
    reports {
        testAggregateTestReport(AggregateTestReport) { (1)
            testSuiteName = "test"
        }
    }
}
1 创建一个名为 testAggregateTestReportAggregateTestReport 类型的报告,聚合所有项目依赖项中具有给定 TestSuite#getName() 的所有测试套件。

报告创建会自动创建支持任务,以按给定的测试套件类型值进行聚合。

依赖管理

测试报告聚合插件添加了以下依赖配置

表 1. 测试报告聚合插件 - 依赖配置
名称 含义

testReportAggregation

用于声明所有具有要聚合的测试结果数据的项目依赖项的配置。

aggregateTestReportResults

使用变体感知匹配来消费 testReportAggregation 配置中的项目依赖项,以查找适当的测试套件类型。

如果项目也应用了 jvm-test-suite 插件,则无需明确将依赖项添加到 testReportAggregation 配置中。