Kotlin Multiplatform parameterized tests with Burst
Unit tests are great. They give us confidence to build and change our software.
Sometimes I want to write a test once and run it for multiple algorithms, inputs, or environments. It’s been possible to do this with JUnit for a long time:
- In JUnit 3 and 4, using Square’s Burst library
- In JUnit 4 and 5, using Google’s TestParameterInjector library
- In JUnit 4 and 5, using the @ParameterizedTest extension
But I’ve missed this feature in my Kotlin Multiplatform projects!
So today we’re announcing Burst 2.0, a Gradle plug-in that adds support for parameterized tests to Kotlin/Multiplatform projects. It is a spiritual successor to Square’s Burst library, so we’re using ‘2.0’ for our first release.
To use Burst:
- Add the Burst plugin to your project’s Gradle build
- Add
@Burst
to your test class - Add parameters to the constructor, test functions, or both.
@Burst
class WebServerTest(
val protocol: Protocol = burstValues(Http1(), Http2(), Http3()),
) {
@Test
fun getRequest() {
dispatchGetRequest(protocol)
}
@Test
fun postRequest() {
dispatchPostRequest(protocol)
}
}
Run the test suite to see results for each combination of parameters:
Use burstValues()
to specify each parameter’s tested values. The first argument is used when you test in the IDE.
It’s not necessary to use burstValues()
for enums and booleans: Burst will test all possible values. Specify a default value to use it when testing in the IDE.
It’s possible to create a lot of tests this way. Burst runs every combination of test parameters, on every Kotlin/Multiplatform target.