你可以使用 IntelliJ 原生匯入器Eclipse Buildship 在 IDE 內開啟此範例。

此範例顯示如何彙總多個 Java 子專案的測試結果。test-report-aggregation 外掛程式 透過一個獨立專案提供此功能,用於指定要納入彙總的哪些子專案。

此範例中的專案包含三個「程式碼」子專案:applicationlistutilities。這三個專案都會套用 java 外掛程式,而 application 會透過其實作組態使用 listutilities。第四個子專案 test-results 是用於收集彙總測試結果的獨立公用程式專案。

測試報告彙總外掛程式目前無法與 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)
            testType = TestSuiteType.UNIT_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)
            testType = TestSuiteType.UNIT_TEST
        }
    }
}

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

獨立專案會套用 test-report-aggregation,但如果沒有 jvm-test-suite 外掛程式(它會由 java 外掛程式自動套用),則需要額外的組態。

在此情況下,需要兩項額外的設定

1 使用 testReportAggregation 組態宣告相依性
2 定義類型為 AggregateTestReport 的報告,用於收集單元測試套件的測試資料
3 (選用)將彙總測試報告產生設為「檢查」生命週期階段的一部分

報告彙總邏輯不會自動檢查所有子專案的測試結果以進行彙總。相反地,會選取 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 專案章節中的測試