Skip to content

Commit

Permalink
Implements SkipTestException
Browse files Browse the repository at this point in the history
Fixes #522
  • Loading branch information
LeoColman committed Jun 4, 2019
1 parent 8ddc032 commit a7dadf8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
18 changes: 17 additions & 1 deletion kotlintest-core/src/main/kotlin/io/kotlintest/TestCase.kt
Expand Up @@ -91,4 +91,20 @@ fun sourceRef(): SourceRef {
return stack.dropWhile {
it.className.startsWith("io.kotlintest")
}[0].run { SourceRef(lineNumber, fileName) }
}
}

/**
* Exception to mark a test as ignored while it is already running
*
* The SkipTestException may be thrown inside a test case to skip it (mark it as ignored). Any subclass of this class
* may be used, in case you want to use your specific exception.
*
* ```
* class FooTest : StringSpec({
* "Ignore this test!" {
* throw SkipTestException("I want to ignore this test!")
* }
* })
* ```
*/
open class SkipTestException(val reason: String? = null): RuntimeException(reason)
Expand Up @@ -191,6 +191,7 @@ class TestCaseExecutor(private val listener: TestEngineListener,
private fun buildTestResult(error: Throwable?, metadata: Map<String, Any?>): TestResult = when (error) {
null -> TestResult(TestStatus.Success, null, null, metadata)
is AssertionError -> TestResult(TestStatus.Failure, error, null, metadata)
is SkipTestException -> TestResult(TestStatus.Ignored, null, error.reason, metadata)
else -> TestResult(TestStatus.Error, error, null, metadata)
}

Expand Down
@@ -0,0 +1,39 @@
package com.sksamuel.kotlintest

import com.nhaarman.mockito_kotlin.argThat
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.then
import io.kotlintest.*
import io.kotlintest.runner.jvm.TestCaseExecutor
import io.kotlintest.runner.jvm.TestEngineListener
import io.kotlintest.specs.FreeSpec
import io.kotlintest.specs.FunSpec
import kotlinx.coroutines.GlobalScope
import java.util.concurrent.Executors

class SkipTestExceptionTest : FunSpec() {

private val scheduler = Executors.newScheduledThreadPool(1)

init {
test("A test that throws SkipTestException should have Ignored as a result") {

val listenerExecutor = Executors.newSingleThreadExecutor()
val listener = mock<TestEngineListener> {}
val executor = TestCaseExecutor(listener, listenerExecutor, scheduler)

val testCase = TestCase.test(Description.spec("wibble"), object : FreeSpec() {}) {
throw SkipTestException("Foo")
}

val context = object : TestContext(GlobalScope.coroutineContext) {
override suspend fun registerTestCase(testCase: TestCase) {}
override fun description(): Description = Description.spec("wibble")
}
executor.execute(testCase, context)

then(listener).should().exitTestCase(argThat { description == Description.spec("wibble") }, argThat { status == TestStatus.Ignored && reason == "Foo" })

}
}
}

0 comments on commit a7dadf8

Please sign in to comment.