Following on from my recent article on Kotlin for Scala developers, in this blog I’ll introduce how you can use the KotlinTest framework to write unit tests in Kotlin, which is a great way to begin exploring Kotlin.

KotlinTest is based on ScalaTest (a superb jUnit like test framework created by Bill Venners) and offers the same choice of testing styles as well as many assertions and helpers for matching through the use of a light DSL.

To use KotlinTest first add the dependencies to your build. We’ll assume you’re using gradle (but obviously Maven is fine too).

compile 'io.kotlintest:kotlintest:1.1.1'

Then we can create a test. Each test must extend from one of the spec classes. Each spec class offers a different way of laying out your tests. Some are very similar and similar to jTest’s @Test annotation, and some are more complicated offering BDD style descriptions.

Testing Styles

Lets give an example of the FlatSpec, which allows you to write tests like this: