kotest DescribeSpec interception
In this note we will have an example of interception/intercepting before and after the kotest test execution.
GT-Sandbox-Snapshot
Code
package com.glassthought.sandbox
import com.glassthought.sandbox.sandbox.util.out.impl.OutSettings
import gt.sandbox.util.output.Out
import io.kotest.core.extensions.TestCaseExtension
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
val out = Out.standard(OutSettings(printCoroutineName = true))
class MyTestExtension : TestCaseExtension {
override suspend fun intercept(
testCase: TestCase,
execute: suspend (TestCase) -> TestResult
): TestResult {
out.printlnBlue("Before executing test: ${testCase.name.testName}")
val result = execute(testCase)
out.printlnGreen("After executing test: ${testCase.name.testName}")
return result
}
}
class MyDescribeSpec : DescribeSpec({
extension(MyTestExtension())
describe("outer-describe") {
describe("nested-describe-1"){
it("it-in-nested-describe") {
out.println("Running test inside inner describe")
}
}
it("test-1 in outer describe") {
out.println("running test-1 in outer describe")
}
it("test-2 in outer describe") {
out.println("Running test-2 in outer describe")
}
}
})
Command to reproduce:
gt.sandbox.checkout.commit 1cd5a2770a5b49285fe4 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew test --rerun-tasks"
Recorded output of command:
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details
> Task :app:checkKotlinGradlePluginConfigurationErrors SKIPPED
> Task :app:processResources NO-SOURCE
> Task :app:processTestResources NO-SOURCE
> Task :app:compileKotlin
> Task :app:compileJava NO-SOURCE
> Task :app:classes UP-TO-DATE
> Task :app:compileTestKotlin
> Task :app:compileTestJava NO-SOURCE
> Task :app:testClasses UP-TO-DATE
> Task :app:test
Gradle Test Executor 1 STANDARD_OUT
Warning: Kotest autoscan is enabled. This means Kotest will scan the classpath for extensions that are annotated with @AutoScan. To avoid this startup cost, disable autoscan by setting the system property 'kotest.framework.classpath.scanning.autoscan.disable=true'. In 6.0 this value will default to true. For further details see https://kotest.io/docs/next/framework/project-config.html#runtime-detection
com.glassthought.sandbox.MyDescribeSpec STANDARD_OUT
Before executing test: outer-describe
com.glassthought.sandbox.MyDescribeSpec > outer-describe STANDARD_OUT
Before executing test: nested-describe-1
com.glassthought.sandbox.MyDescribeSpec > outer-describe > nested-describe-1 STANDARD_OUT
Before executing test: it-in-nested-describe
com.glassthought.sandbox.MyDescribeSpec > outer-describe > nested-describe-1 > com.glassthought.sandbox.MyDescribeSpec.it-in-nested-describe STARTED
com.glassthought.sandbox.MyDescribeSpec > outer-describe > nested-describe-1 > com.glassthought.sandbox.MyDescribeSpec.it-in-nested-describe STANDARD_OUT
Running test inside inner describe
After executing test: it-in-nested-describe
com.glassthought.sandbox.MyDescribeSpec > outer-describe > nested-describe-1 > com.glassthought.sandbox.MyDescribeSpec.it-in-nested-describe PASSED
com.glassthought.sandbox.MyDescribeSpec > outer-describe > nested-describe-1 STANDARD_OUT
After executing test: nested-describe-1
com.glassthought.sandbox.MyDescribeSpec > outer-describe STANDARD_OUT
Before executing test: test-1 in outer describe
com.glassthought.sandbox.MyDescribeSpec > outer-describe > test-1 in outer describe STARTED
com.glassthought.sandbox.MyDescribeSpec > outer-describe > test-1 in outer describe STANDARD_OUT
running test-1 in outer describe
After executing test: test-1 in outer describe
com.glassthought.sandbox.MyDescribeSpec > outer-describe > test-1 in outer describe PASSED
com.glassthought.sandbox.MyDescribeSpec > outer-describe STANDARD_OUT
Before executing test: test-2 in outer describe
com.glassthought.sandbox.MyDescribeSpec > outer-describe > test-2 in outer describe STARTED
com.glassthought.sandbox.MyDescribeSpec > outer-describe > test-2 in outer describe STANDARD_OUT
Running test-2 in outer describe
After executing test: test-2 in outer describe
com.glassthought.sandbox.MyDescribeSpec > outer-describe > test-2 in outer describe PASSED
com.glassthought.sandbox.MyDescribeSpec > outer-describe STANDARD_OUT
After executing test: outer-describe
BUILD SUCCESSFUL in 9s
3 actionable tasks: 3 executed
Backlinks