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.

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.

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 at https://kotlinlang.org/docs/power-assert.html which gives up-to-date detailed guide on how to set it up and use it.

Deep-Dive Resources

For a comprehensive exploration of Kotlin’s power-assert, Duncan McGregor’s videos offer invaluable insights:

These videos delve into practical examples and considerations when leveraging 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.