← Back to Kotlin Course | Chapter 13: Error Handling & Testing | Lesson 5 of 6

Introduction to Kotest

Kotest is another testing helper, built specifically with Kotlin in mind, offering a more expressive, story-like way to write checks.

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

markup
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
    }
})

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

markup
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
    }
})

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

markup
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" }
        }
    }
})

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

markup
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
    }
})
Common Mistakes
  1. Assuming Kotest is a drop-in replacement usable without its own Gradle dependency and test engine configuration.
  2. Mixing up Kotest's various testing styles (StringSpec, FunSpec, BehaviorSpec) inconsistently across a project without a clear convention.
  3. Forgetting Kotest's matcher syntax (shouldBe, shouldContain) is provided by its own assertion library, distinct from JUnit's assertEquals style.
Chapter Summary
  • Kotest is a Kotlin-specific testing framework offering multiple testing styles and expressive matchers.
  • StringSpec is one of Kotest's simplest styles, where each test is a string description followed by a lambda.
  • Kotest matchers like shouldBe and shouldContain provide a readable, fluent way to write assertions.
  • Like JUnit, Kotest requires its own Gradle/Maven dependencies and cannot run from a plain .kt file alone.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.