OhMad
OhMad

Reputation: 7289

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?

Upvotes: 6

Views: 4053

Answers (2)

Tonnie
Tonnie

Reputation: 8112

This is now well outlined on the Kotlin Docs on how to set-up Jetbrains Kotlin Test JUnit on Intellij using Kotlin DSL.

Step 1 - Add this line to your dependencies i.e. inside build.gradle.kts file

dependencies { 
...

testImplementation(kotlin("test"))
...

}

Step 2 Add the test task block i.e. inside build.gradle(.kts) file:

  tasks.test {
    useJUnitPlatform()
}

Otherwise, try using this dependency with the latest Kotlin Version. For me this was Kotlin version 1.9.10.

testImplementation ("org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion")

Upvotes: 0

holi-java
holi-java

Reputation: 30686

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.

Upvotes: 10

Related Questions