您可以在支援 Gradle 的 IDE 中開啟此範例。

此範例示範如何彙總多個 Java 子專案的測試結果。test-report-aggregation 外掛透過獨立專案提供此功能,用於指定包含哪些子專案以進行彙總。

此範例中的專案包含三個「程式碼」子專案:applicationlistutilities。所有三個專案都應用了 java 外掛,並且 application 透過其實作配置使用 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 專案章節中的測試