From a20b16da44e1f87e9c6e167c6177f3f83915f6f8 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sun, 9 Apr 2017 09:37:36 -0700 Subject: [PATCH] Extract TestNG results and show them in the final report. --- .../beust/kobalt/internal/GenericRunner.kt | 7 ++-- .../com/beust/kobalt/internal/TestNgRunner.kt | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt index 9819d1f3..01836be1 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt @@ -16,12 +16,14 @@ abstract class GenericTestRunner: ITestRunnerContributor { abstract val mainClass: String abstract val annotationPackage: String abstract val runnerName: String - open val shortMessage: String? = null//""Short message" - open val longMessage: String? = null//""Long message" + open var shortMessage: String? = null + open var longMessage: String? = null abstract fun args(project: Project, context: KobaltContext, classpath: List, testConfig: TestConfig) : List + open fun onFinish(project: Project) {} + open val extraClasspath: List = emptyList() open fun filterTestClasses(classes: List) : List = classes @@ -145,6 +147,7 @@ abstract class GenericTestRunner: ITestRunnerContributor { throw KobaltException("Couldn't find a test configuration named \"$configName\"") } + onFinish(project) return TestResult(result, shortMessage, longMessage) } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt index afe32430..345919c9 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt @@ -13,8 +13,12 @@ import org.testng.remote.strprotocol.JsonMessageSender import org.testng.remote.strprotocol.MessageHelper import org.testng.remote.strprotocol.MessageHub import org.testng.remote.strprotocol.TestResultMessage +import org.w3c.dom.Attr +import org.xml.sax.InputSource import java.io.File +import java.io.FileReader import java.io.IOException +import javax.xml.parsers.DocumentBuilderFactory class TestNgRunner : GenericTestRunner() { @@ -61,6 +65,35 @@ class TestNgRunner : GenericTestRunner() { } } + /** + * Extract test results from testng-results.xml and initialize shortMessage. + */ + override fun onFinish(project: Project) { + File(defaultOutput(project), "testng-results.xml").let { file -> + val ins = InputSource(FileReader(file)) + val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(ins) + + val root = doc.documentElement + var failed = 0 + var skipped = 0 + var passed = 0 + repeat(root.attributes.length) { + val attribute = root.attributes.item(it) + if (attribute is Attr) when (attribute.name) { + "failed" -> failed = Integer.parseInt(attribute.value) + "skipped" -> skipped = Integer.parseInt(attribute.value) + "passed" -> passed = Integer.parseInt(attribute.value) + } + } + + if (failed == 0) { + shortMessage = "$passed tests" + } else if (failed > 0) { + shortMessage = "$failed failed" + (if (skipped > 0) ", $skipped skipped" else "") + " tests" + } + } + } + val VERSION_6_10 = StringVersion("6.10") fun _runTests(project: Project, context: KobaltContext, classpath: List,