Introduction to Kotest
In this page:
A Basic Kotest StringSpec Test
Kotest's StringSpec style lets you write a test as a plain string description followed by a lambda body, reading almost like a sentence describing the expected behavior.
Warning: This example uses a third-party testing/mocking library that is not part of the plain Kotlin standard library and must be added as a project dependency (e.g. via Gradle) -- it cannot run in a plain kotlinc sandbox with no dependencies.
Example: A Basic Kotest StringSpec Test
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
class CalculatorSpec : StringSpec({
"adding two numbers returns their sum" {
(2 + 3) shouldBe 5
}
})
Login to try C/C++/Java/PHP code in the editor
Using Kotest Matchers
Kotest provides expressive matcher functions like shouldBe, shouldContain, and shouldThrow, which read more like natural language than traditional assertEquals-style calls.
Warning: This example uses a third-party testing/mocking library that is not part of the plain Kotlin standard library and must be added as a project dependency (e.g. via Gradle) -- it cannot run in a plain kotlinc sandbox with no dependencies.
Example: Using Kotest Matchers
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.shouldBe
class ListSpec : StringSpec({
"a list of fruits should contain banana" {
val fruits = listOf("apple", "banana", "cherry")
fruits shouldContain "banana"
fruits.size shouldBe 3
}
})
Login to try C/C++/Java/PHP code in the editor
Testing for Exceptions
Kotest's shouldThrow<ExceptionType> { } matcher checks that a block of code throws a specific exception type, failing the test clearly if it does not.
Warning: This example uses a third-party testing/mocking library that is not part of the plain Kotlin standard library and must be added as a project dependency (e.g. via Gradle) -- it cannot run in a plain kotlinc sandbox with no dependencies.
Example: Testing for Exceptions
import io.kotest.core.spec.style.StringSpec
import io.kotest.assertions.throwables.shouldThrow
class ValidationSpec : StringSpec({
"negative age should throw an exception" {
shouldThrow<IllegalArgumentException> {
require(-1 >= 0) { "Age cannot be negative" }
}
}
})
Login to try C/C++/Java/PHP code in the editor
Kotest's Multiple Testing Styles
Beyond StringSpec, Kotest offers other styles like FunSpec and BehaviorSpec for different preferences in structuring tests, all requiring the same underlying Kotest Gradle dependency and test engine setup.
Warning: This example uses a third-party testing/mocking library that is not part of the plain Kotlin standard library and must be added as a project dependency (e.g. via Gradle) -- it cannot run in a plain kotlinc sandbox with no dependencies.
Example: Kotest's Multiple Testing Styles
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class MathSpec : FunSpec({
test("multiplying two numbers returns their product") {
(4 * 5) shouldBe 20
}
})
Login to try C/C++/Java/PHP code in the editor
- Assuming Kotest is a drop-in replacement usable without its own Gradle dependency and test engine configuration.
- Mixing up Kotest's various testing styles (StringSpec, FunSpec, BehaviorSpec) inconsistently across a project without a clear convention.
- Forgetting Kotest's matcher syntax (
shouldBe,shouldContain) is provided by its own assertion library, distinct from JUnit'sassertEqualsstyle.
- Kotest is a Kotlin-specific testing framework offering multiple testing styles and expressive matchers.
StringSpecis one of Kotest's simplest styles, where each test is a string description followed by a lambda.- Kotest matchers like
shouldBeandshouldContainprovide a readable, fluent way to write assertions. - Like JUnit, Kotest requires its own Gradle/Maven dependencies and cannot run from a plain
.ktfile alone.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: