Search code examples
unit-testingtestingkotlin

How to Write Kotlin Unit Test with kotlin-test?


I am trying to test my code using Kotlin. When trying to import the kotlin.test package, nothing appears.

Do I have to add the dependency somewhere first? And how can I run my unit test afterwards?


Solution

  • The kotlin.test package is packaged in kotlin-test module, so you need add the dependencies in your build.gradle as below:

    dependencies{
       testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
    }
    

    Beside that, you need to add an unit test framework in the build.gradle like as junit-4, for example:

    dependencies{
       testCompile "junit:junit:4.12"
    }
    

    How to write a JUnit Test? you can see it in junit.org as further.