Power-assert compiler plugin
The Kotlin Power-assert compiler plugin improves the debugging experience by providing detailed failure messages with contextual information. It simplifies the process of writing tests by automatically generating intermediate values in failure messages. It helps you understand why a test failed without needing complex assertion libraries.
This is an example message provided by the plugin:
The Power-assert plugin key features:
Enhanced error messages: The plugin captures and displays the values of variables and sub-expressions within the assertion to clearly identify the cause of failure.
Simplified testing: Automatically generates informative failure messages, reducing the need for complex assertion libraries.
Support for multiple functions: By default, it transforms
assert()
function calls but can also transform other functions, such asrequire()
,check()
, andassertTrue()
.
Apply the plugin
To enable the Power-assert plugin, configure your build.gradle(.kts)
file as follows:
Configure the plugin
The Power-assert plugin provides several options to customize its behavior:
functions
: A list of fully-qualified function paths. The Power-assert plugin will transform the calls to these functions. If not specified, onlykotlin.assert()
calls will be transformed by default.includedSourceSets
: A list of Gradle source sets that the Power-assert plugin will transform. If not specified, all test source sets will be transformed by default.
To customize the behavior, add the powerAssert {}
block to you build script file:
Since the plugin is Experimental, you will see warnings every time you build your app. To exclude these warnings, add this @OptIn
annotation before declaring the powerAssert {}
block:
Use the plugin
This section provides examples of using the Power-assert compiler plugin.
See the complete code of the build script file build.gradle.kts
for all these examples:
Assert function
Consider the following test with the assert()
function:
If you run the testFunction()
test with the Power-assert plugin enabled, you get the explicit failure message:
To get a more complete error message, always inline the variable into the test function parameters. Consider the following test function:
The output of the executed code doesn't provide enough information to find the cause of the problem:
Inline the variable into the assert()
function:
After execution, you get more explicit information about what went wrong:
Beyond assert function
The Power-assert plugin can transform various functions beyond assert
which is transformed by default. Functions like require()
, check()
, assertTrue()
, assertEqual()
and others can also be transformed, if they have a form that allows taking a String
or () -> String
value as the last parameter.
Before using a new function in a test, specify the function in the powerAssert {}
block of your build script file. For example, the require()
function:
After adding the function, you can use it in your tests:
The output for this example uses the Power-assert plugin to provide detailed information about the failed test:
The message shows the intermediate values that lead to the failure, making it easier to debug.
Soft assertions
The Power-assert plugin supports soft assertions, which do not immediately fail the test but instead collect assertion failures and report them at the end of the test run. This can be useful when you want to see all assertion failures in a single run without stopping at the first failure.
To enable soft assertions, implement the way you will collect error messages:
Add these functions to the powerAssert {}
block to make them available for the Power-assert plugin:
After that, you could use it in your test code:
In the output, all the assert()
function error messages will be printed one after another:
What's next
Look through a simple project with the plugin enabled and a more complex project with multiple source sets.