1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Initial support for JUnit 5.

This commit is contained in:
Cedric Beust 2017-04-07 11:42:29 -07:00
parent 5768fb24db
commit 33dafe6cd0
7 changed files with 171 additions and 9 deletions

View file

@ -15,9 +15,13 @@ abstract class GenericTestRunner: ITestRunnerContributor {
abstract val dependencyName : String abstract val dependencyName : String
abstract val mainClass: String abstract val mainClass: String
abstract val annotationPackage: String abstract val annotationPackage: String
abstract val runnerName: String
abstract fun args(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>, abstract fun args(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>,
testConfig: TestConfig) : List<String> testConfig: TestConfig) : List<String>
open val extraClasspath: List<String> = emptyList()
open fun filterTestClasses(classes: List<String>) : List<String> = classes open fun filterTestClasses(classes: List<String>) : List<String> = classes
override fun run(project: Project, context: KobaltContext, configName: String, override fun run(project: Project, context: KobaltContext, configName: String,
@ -98,7 +102,7 @@ abstract class GenericTestRunner: ITestRunnerContributor {
configName: String) : Boolean { configName: String) : Boolean {
var result = false var result = false
context.logger.log(project.name, 1, "Running default TestNG runner") context.logger.log(project.name, 1, "Running tests with " + runnerName)
val testConfig = project.testConfigs.firstOrNull { it.name == configName } val testConfig = project.testConfigs.firstOrNull { it.name == configName }
@ -144,13 +148,14 @@ abstract class GenericTestRunner: ITestRunnerContributor {
*/ */
@VisibleForTesting @VisibleForTesting
fun calculateAllJvmArgs(project: Project, context: KobaltContext, fun calculateAllJvmArgs(project: Project, context: KobaltContext,
testConfig: TestConfig, classpath: List<IClasspathDependency>, pluginInfo: IPluginInfo) : List<String> { testConfig: TestConfig, classpath: List<IClasspathDependency>, pluginInfo: IPluginInfo) : List<String> {
val fullClasspath = classpath.map { it.jarFile.get().absolutePath } + extraClasspath
// Default JVM args // Default JVM args
val jvmFlags = arrayListOf<String>().apply { val jvmFlags = arrayListOf<String>().apply {
addAll(testConfig.jvmArgs) addAll(testConfig.jvmArgs)
add("-ea") add("-ea")
add("-classpath") add("-classpath")
add(classpath.map { it.jarFile.get().absolutePath }.joinToString(File.pathSeparator)) add(fullClasspath.joinToString(File.pathSeparator))
} }
// JVM flags from the contributors // JVM flags from the contributors

View file

@ -0,0 +1,151 @@
package com.beust.kobalt.internal
import com.beust.jcommander.JCommander
import com.beust.jcommander.Parameter
import com.beust.kobalt.TestConfig
import com.beust.kobalt.api.IAffinity
import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.KobaltLogger
import com.google.inject.Inject
import org.junit.platform.engine.TestExecutionResult
import org.junit.platform.engine.discovery.DiscoverySelectors
import org.junit.platform.engine.reporting.ReportEntry
import org.junit.platform.engine.support.descriptor.MethodSource
import org.junit.platform.launcher.LauncherDiscoveryRequest
import org.junit.platform.launcher.TestExecutionListener
import org.junit.platform.launcher.TestIdentifier
import org.junit.platform.launcher.TestPlan
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder
import org.junit.platform.launcher.core.LauncherFactory
import java.io.File
import java.nio.file.Paths
/**
* Runner for JUnit 5 tests. This class also contains a main() entry point since JUnit 5 no longer supplies one.
*/
class JUnit5Runner @Inject constructor(kFiles: KFiles) : GenericTestRunner() {
override val dependencyName = "jupiter"
override val annotationPackage = "org.junit.jupiter.api"
override val mainClass = "com.beust.kobalt.internal.JUnit5RunnerKt"
override val runnerName = "JUnit 5"
override fun affinity(project: Project, context: KobaltContext) : Int {
val result =
if (project.testDependencies.any { it.id.contains("jupiter") }) IAffinity.DEFAULT_POSITIVE_AFFINITY + 100
else 0
return result
}
override fun args(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>, testConfig: TestConfig): List<String> {
val testClassDir = KFiles.joinDir(project.buildDirectory, KFiles.TEST_CLASSES_DIR)
val classDir = KFiles.joinDir(project.buildDirectory, KFiles.CLASSES_DIR)
val args = listOf("--testClassDir", testClassDir,
"--classDir", classDir,
"--log", KobaltLogger.LOG_LEVEL.toString())
return args
}
override val extraClasspath = kFiles.kobaltJar
}
private class Args {
@Parameter(names = arrayOf("--log"))
var log: Int = 1
@Parameter(names = arrayOf("--testClassDir"))
var testClassDir: String = "kobaltBuild/test-classes"
@Parameter(names = arrayOf("--classDir"))
var classDir: String = "kobaltBuild/classes"
}
fun main(argv: Array<String>) {
val args = Args()
val jc = JCommander(args)
jc.parse(*argv)
val testClassDir = File(args.testClassDir).absolutePath
val classDir = File(args.classDir).absolutePath
val request : LauncherDiscoveryRequest = LauncherDiscoveryRequestBuilder()
.selectors(DiscoverySelectors.selectClasspathRoots(setOf(
Paths.get(testClassDir),
Paths.get(classDir)
)))
.selectors(DiscoverySelectors.selectDirectory(testClassDir))
.build()
fun testName(id: TestIdentifier) : String? {
val result =
if (id.source.isPresent) {
val source = id.source.get()
if (source is MethodSource) {
source.className + "." + source.methodName
} else {
null
}
} else {
null
}
return result
}
var passed = 0
var failed = 0
var skipped = 0
var aborted = 0
fun log(level: Int, s: String) {
if (level <= args.log) println(s)
}
val listener = object: TestExecutionListener {
override fun executionFinished(testIdentifier: TestIdentifier, testExecutionResult: TestExecutionResult) {
val testName = testName(testIdentifier)
if (testName != null) {
when(testExecutionResult.status) {
TestExecutionResult.Status.FAILED -> {
log(1, "FAILED: $testName, reason: " + testExecutionResult.throwable.get().toString())
failed++
}
TestExecutionResult.Status.ABORTED -> {
log(1, "ABORTED: $testName, reason: " + testExecutionResult.throwable.get().toString())
aborted++
}
TestExecutionResult.Status.SUCCESSFUL -> {
log(2, "PASSED: $testName")
passed++
} else -> {
}
}
}
}
override fun executionSkipped(testIdentifier: TestIdentifier, reason: String) {
testName(testIdentifier)?.let {
log(1, "Skipping $it because $reason")
skipped++
}
}
override fun executionStarted(testIdentifier: TestIdentifier) {
testName(testIdentifier)?.let {
log(2, "Starting $it")
}
}
override fun testPlanExecutionStarted(testPlan: TestPlan?) {}
override fun dynamicTestRegistered(testIdentifier: TestIdentifier?) {}
override fun reportingEntryPublished(testIdentifier: TestIdentifier?, entry: ReportEntry?) {}
override fun testPlanExecutionFinished(testPlan: TestPlan?) {}
}
LauncherFactory.create().execute(request, listener)
log(1, "TEST RESULTS: $passed PASSED, $failed FAILED, $skipped SKIPPED, $aborted ABORTED")
}

View file

@ -8,10 +8,9 @@ import com.beust.kobalt.api.Project
open class JUnitRunner() : GenericTestRunner() { open class JUnitRunner() : GenericTestRunner() {
override val mainClass = "org.junit.runner.JUnitCore" override val mainClass = "org.junit.runner.JUnitCore"
override val annotationPackage = "org.junit" override val annotationPackage = "org.junit"
override val dependencyName = "junit" override val dependencyName = "junit"
override val runnerName = "JUnit 4"
override fun args(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>, override fun args(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>,
testConfig: TestConfig) = findTestClasses(project, context, testConfig) testConfig: TestConfig) = findTestClasses(project, context, testConfig)

View file

@ -6,6 +6,7 @@ package com.beust.kobalt.internal
*/ */
class KotlinTestRunner : JUnitRunner() { class KotlinTestRunner : JUnitRunner() {
override val dependencyName = "io.kotlintest" override val dependencyName = "io.kotlintest"
override val runnerName = "Kotlin Test"
/** /**
* KotlinTestRunner runs tests in the init{} initializer, so ignore all the extra * KotlinTestRunner runs tests in the init{} initializer, so ignore all the extra

View file

@ -6,5 +6,6 @@ package com.beust.kobalt.internal
*/ */
class SpekRunner : JUnitRunner() { class SpekRunner : JUnitRunner() {
override val dependencyName = "org.jetbrains.spek" override val dependencyName = "org.jetbrains.spek"
override val runnerName = "Spek"
} }

View file

@ -2,21 +2,25 @@ package com.beust.kobalt.internal
import com.beust.kobalt.AsciiArt import com.beust.kobalt.AsciiArt
import com.beust.kobalt.TestConfig import com.beust.kobalt.TestConfig
import com.beust.kobalt.api.* import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project
import com.beust.kobalt.maven.aether.AetherDependency import com.beust.kobalt.maven.aether.AetherDependency
import com.beust.kobalt.misc.* import com.beust.kobalt.misc.*
import org.testng.remote.RemoteArgs import org.testng.remote.RemoteArgs
import org.testng.remote.strprotocol.* 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 java.io.File import java.io.File
import java.io.IOException import java.io.IOException
class TestNgRunner : GenericTestRunner() { class TestNgRunner : GenericTestRunner() {
override val mainClass = "org.testng.TestNG" override val mainClass = "org.testng.TestNG"
override val dependencyName = "testng" override val dependencyName = "testng"
override val annotationPackage = "org.testng" override val annotationPackage = "org.testng"
override val runnerName = "TestNG"
fun defaultOutput(project: Project) = KFiles.joinDir(project.buildDirectory, "test-output") fun defaultOutput(project: Project) = KFiles.joinDir(project.buildDirectory, "test-output")

View file

@ -24,6 +24,7 @@
<class-name>com.beust.kobalt.internal.TestNgRunner</class-name> <class-name>com.beust.kobalt.internal.TestNgRunner</class-name>
<class-name>com.beust.kobalt.internal.SpekRunner</class-name> <class-name>com.beust.kobalt.internal.SpekRunner</class-name>
<class-name>com.beust.kobalt.internal.KotlinTestRunner</class-name> <class-name>com.beust.kobalt.internal.KotlinTestRunner</class-name>
<class-name>com.beust.kobalt.internal.JUnit5Runner</class-name>
<!-- Templates --> <!-- Templates -->
<class-name>com.beust.kobalt.app.KobaltPluginTemplate</class-name> <class-name>com.beust.kobalt.app.KobaltPluginTemplate</class-name>