Power Assert
Power Assert support was introduced in Kotest 6.0 that enhances assertion failure messages by providing detailed information about the values of each part of an expression when an assertion fails. This makes it easier to understand why an assertion failed without having to add additional debug statements.
How It Works​
When an assertion fails, Power Assert shows the values of each part of the expression in the error message, making it clear what went wrong. This is particularly useful for complex expressions with method calls or property access chains.
For example, consider this assertion:
val hello = "Hello"
val world = "world!"
hello.substring(1, 3) shouldBe world.substring(1, 4)
Without Power Assert, the error message would simply be:
expected:<"orl"> but was:<"el">
With Power Assert enabled, the error message becomes much more informative:
hello.substring(1, 3) shouldBe world.substring(1, 4)
| | | |
| | | orl
| | world!
| el
Hello
expected:<"orl"> but was:<"el">
This detailed output shows the values of each part of the expression, making it immediately clear what's happening:
hellois "Hello"hello.substring(1, 3)is "el"worldis "world!"world.substring(1, 4)is "orl"
Setup​
Power Assert is implemented as a Kotlin compiler plugin that's part of Kotlin 2.0+. To use with Kotest you have two options.
With the Gradle Plugin​
If using Kotest 6.2 and higher, and the Kotest Gradle plugin is enabled, then the plugin will automatically configure Power Assert for you if required.
- Add the Kotest Gradle plugin to your build:
plugins {
id("io.kotest") version "<kotest-version>"
}
- Configure the Kotest Gradle plugin to enable Power Assert:
kotest {
enablePowerAssert = true
}
Adding the Plugin Manually​
- Add the Power Assert plugin to your build:
plugins {
id("org.jetbrains.kotlin.plugin.power-assert") version "<kotlin-version>"
}
- Add the Assertions library to your dependencies:
dependencies {
testImplementation("io.kotest:kotest-assertions-core:<kotest-version>")
}
- Configure which assertion functions should be enhanced with Power Assert, which in this case is the Kotest
shouldBematchers:
powerAssert {
functions = listOf("io.kotest.matchers.shouldBe")
}