Kotlin, as a modern, expressive language, continues to push the boundaries of developer productivity and code quality. Among its latest features is the power-assert compiler plugin, an assertion plugin designed to improve the way we write and debug tests. Originally developed by Brian Norman, this plugin has recently been incorporated into Kotlin as an experimental feature.
In this blog post, we’ll scrape the surface of what Kotlin power-assert is, how it works, and why you might consider incorporating it into your development workflow.
What is Kotlin Power-Assert?
Kotlin power-assert is a compiler plugin that enhances assertion statements in your tests by providing detailed feedback when an assertion fails. Traditional assertions often leave developers with vague messages like “expected true but was false”, which can be frustrating and time-consuming to debug. Power-assert aims to solve this by offering more context and clarity.
Setting Up Power-Assert
To start using power-assert in your Kotlin project, you’ll need to enable the plugin. Check out the official Kotlin documentation on power-assert which gives up-to-date detailed guide on how to set it up and use it.
How Does Power-Assert Work?
When an assertion fails, power-assert provides a detailed visual representation of the failed expression. This includes the values of variables and the structure of the expression at the time of failure. By doing so, it helps developers quickly pinpoint the cause of the failure without needing to manually insert print statements or debug through the code.
For example, consider the following traditional assertion:
1
assert(a * b == c + d)
If this assertion fails, a standard error message might tell you that the expression is false, but it won’t provide much context. With power-assert, the error message will break down the expression and show the values of a, b, c, and d, helping you pinpoint the cause of the failure more quickly.
And so you will see:
1
2
3
4
5
6
7
8
9
10
Assertion failed
assert(a * b == c + d)
| | | | | | |
| | | | | | 1
| | | | | 11
| | | | 10
| | | false
| | 1
| 12
12
This visualization clearly shows the values of a, b, c and d therefore making it immediately obvious why the assertion failed.
Asserting On Complex Values
Consider the following data class and test
1
2
3
4
5
6
7
8
9
10
11
12
data class Person(
val firstName: String,
val lastName: String,
val age: Int
)
@Test
fun `power assert complex values`() {
val charlie = Person("Charlie", "Brown", 23)
val sally = Person("Sally", "Gray", 21)
assert(charlie.age == sally.age)
}
The plugin will show value for each of the objects and fields in the assertion, and will render the following:
1
2
3
4
5
6
7
8
Assertion failed
assert(charlie.age == sally.age)
| | | | |
| | | | 21
| | | Person(firstName=Sally, lastName=Gray, age=21)
| | false
| 23
Person(firstName=Charlie, lastName=Brown, age=23)
This output clearly shows how power-assert type of assertion can help with providing more context around tested expressions.
Deep-Dive Resources
For a more comprehensive exploration of Kotlin’s power-assert have a look at the most recent talk from KotlinConf’24 by Brian Norman:
Also Duncan McGregor’s videos offer invaluable insights:
These videos delve into practical examples, highlight some problems and considerations when using power-assert.
Conclusion
Kotlin power-assert is an experimental yet promising feature that can significantly enhance the way we write and debug tests. By offering more informative and readable error messages, it helps developers quickly identify and fix issues. Although power-assert is not a new concept and has been implemented for several languages already (e.g. Java, Clojure, JS, Go), it’s great to see it making it’s way into Kotlin.
As Kotlin continues to evolve, tools like power-assert demonstrate the JetBrains commitment to providing developers with the best possible tools and features. Whether you’re a seasoned Kotlin developer or just getting started, incorporating power-assert into your testing strategy is worth considering.