Kotest
Testing Styles
Kotest offers different Testing Styles
Hello world
Hello world example
From Hello world Test Describe Spec Example
Go to text →
Gradle changes
val kotestVersion = "4.6.3"
implementation("io.kotest:kotest-framework-engine:$kotestVersion")
implementation("io.kotest:kotest-assertions-core:$kotestVersion")
implementation("io.kotest:kotest-runner-junit5:$kotestVersion")
Code
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.ints.shouldBeExactly
import io.kotest.matchers.shouldBe
// Test using one of Kotest DSLs.
class HelloKotestTestWorld: DescribeSpec({
describe("WHEN I land on earth") {
it("TRUE should be true") {
true shouldBe true
}
describe("AND if I am doing math") {
val result = 5
it("THEN 5 should be equal to 3+2") {
result shouldBeExactly 3 + 2
}
}
}
})
Execution order
Execution Order
From execution-order-in-DescribeSpec
Go to text →
z
Recorded output of command:
[INFO][elapsed: 9ms][2️⃣][①][coroutname:@coroutine#3] [1]: Describe-Level-1
[INFO][elapsed: 30ms][2️⃣][①][coroutname:@coroutine#3] [2]: BeforeEach IN Describe-Level-1
[INFO][elapsed: 32ms][2️⃣][①][coroutname:@coroutine#3] [3]: IT-1 in Describe-Level-1
[INFO][elapsed: 33ms][2️⃣][①][coroutname:@coroutine#3]
[INFO][elapsed: 39ms][2️⃣][①][coroutname:@coroutine#3] [4]: Describe-Level-2
[INFO][elapsed: 41ms][2️⃣][①][coroutname:@coroutine#3] [5]: BeforeEach IN Describe-Level-1
[INFO][elapsed: 41ms][2️⃣][①][coroutname:@coroutine#3] [6]: BeforeEach IN Describe-Level-2
[INFO][elapsed: 41ms][2️⃣][①][coroutname:@coroutine#3] [7]: IT-1 in Describe-Level-2
[INFO][elapsed: 41ms][2️⃣][①][coroutname:@coroutine#3]
[INFO][elapsed: 43ms][2️⃣][①][coroutname:@coroutine#3] [8]: BeforeEach IN Describe-Level-1
[INFO][elapsed: 43ms][2️⃣][①][coroutname:@coroutine#3] [9]: BeforeEach IN Describe-Level-2
[INFO][elapsed: 43ms][2️⃣][①][coroutname:@coroutine#3] [10]: IT-2 in Describe-Level-2
[INFO][elapsed: 44ms][2️⃣][①][coroutname:@coroutine#3]
[INFO][elapsed: 45ms][2️⃣][①][coroutname:@coroutine#3] [11]: BeforeEach IN Describe-Level-1
[INFO][elapsed: 46ms][2️⃣][①][coroutname:@coroutine#3] [12]: IT-2 in Describe-Level-1
[INFO][elapsed: 46ms][2️⃣][①][coroutname:@coroutine#3]
Code
package com.glassthought.sandbox
import com.glassthought.sandbox.impl.CustomDescribeSpec
import com.glassthought.sandbox.impl.out
class KotestExecutionOrderTest : CustomDescribeSpec({
describe("Describe-Level-1") {
incrementCountAndPrint("Describe-Level-1")
beforeEach {
incrementCountAndPrint("BeforeEach IN Describe-Level-1")
}
it("IT-1 in Describe-Level-1") {
incrementAndPrintForIt("IT-1 in Describe-Level-1")
}
describe("Describe-Level-2") {
beforeEach {
incrementCountAndPrint("BeforeEach IN Describe-Level-2")
}
incrementCountAndPrint("Describe-Level-2")
it("IT-1 in Describe-Level-2") {
incrementAndPrintForIt("IT-1 in Describe-Level-2")
}
it("IT-2 in Describe-Level-2") {
incrementAndPrintForIt("IT-2 in Describe-Level-2")
}
}
it("IT-2 in Describe-Level-1") {
incrementAndPrintForIt("IT-2 in Describe-Level-1")
}
}
})
private var count = 0
private suspend fun incrementCountAndPrint(string: String) {
count += 1
out.info("[${count}]: ${string}")
}
private suspend fun incrementAndPrintForIt(msg: String) {
incrementCountAndPrint(msg)
out.info("")
}
Command to reproduce:
gt.sandbox.checkout.commit 9ad8f194aba389b94ce8 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew test --rerun-tasks"
Parallel
From Parallelism
Go to text →
Children