A Kotlin Multiplatform library that provides Behavior Driven Development (BDD) extensions for Kotest. Write expressive tests using Given/When/Then or Expect syntax across JVM, iOS, macOS, Linux, and Windows platforms. Though it is true Kotest has a BehaviorSpec, they are kind of hard to read with lots of nesting. This is a simple setup that just allows you to label each section of your test.
- 🎯 BDD Syntax: Write tests using natural Given/When/Then or Expect blocks
- 🌐 Multiplatform: Supports JVM, iOS, macOS, Linux, and Windows
- 📊 Data-driven Testing: Built-in support for parameterized tests with
row()function - 🔧 Kotest Integration: Seamlessly integrates with Kotest's powerful testing framework
- ✅ Type Safety: Fully type-safe with Kotlin's strong type system
- Kotlin: 1.9.0 or higher
- Kotest: 5.0.0 or higher
This package is published to Maven Central, so you can add it to your project as follows:
dependencies {
testImplementation("dev.ktool:kotest-bdd:VERSION")
}Replace VERSION with the latest version from the releases page.
Because of changes to Kotest, you have to use the correct major version corresponding to your version of Kotest.
| Kotest Version | Kotest BDD Version |
|---|---|
| 5.x.x | 1.x.x |
| 6.x.x | 2.x.x |
Create a test class extending BddSpec:
import dev.ktool.kotest.BddSpec
import io.kotest.matchers.shouldBe
class CalculatorSpec : BddSpec({
"adding two numbers" {
Given
val a = 5
val b = 3
When
val result = a + b
Then
result shouldBe 8
}
})For simpler tests, you can use the Expect syntax instead of When/Then:
class SimpleSpec : BddSpec({
"validating user input" {
Given
val email = "user@example.com"
Expect
email.contains("@") shouldBe true
}
})Use the row() function for parameterized tests:
class MathSpec : BddSpec({
"multiplication table"(
row(2, 3, 6),
row(4, 5, 20),
row(7, 8, 56)
) { (a, b, expected) ->
Given("two numbers $a and $b")
When("they are multiplied")
val result = a * b
Then("the result should be $expected")
result shouldBe expected
}
})Chain multiple conditions with And:
class UserSpec : BddSpec({
"user registration" {
Given
val user = User("john", "john@example.com")
And
user.email.contains("@") shouldBe true
When
val registered = userService.register(user)
Then
registered shouldBe true
}
})The library enforces proper BDD structure:
- Given must come before When/Then or Expect
- When must come before Then (cannot be used with Expect)
- Then must come after When (cannot be used with Expect)
- Expect can be used instead of When/Then combination
- And can only be used after Given or When blocks
- Each test must have at least one Then or Expect block
- JVM (Java 17+)
- iOS (x64, ARM64, Simulator ARM64)
- macOS (x64, ARM64)
- Linux (x64, ARM64)
- Windows (x64)
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- Kotest - The underlying testing framework
- Kotlin Multiplatform - Cross-platform development