How to split unit and integration tests in Kotlin and Gradle?

If you want to split unit and integration tests in Kotlin and Gradle, do the following:

  • Create a folder src/integrationTest/kotlin
  • Add the following to your build.gradle.kts:
sourceSets {
  create("integrationTest") {
    kotlin {
      compileClasspath += main.get().output + configurations.testRuntimeClasspath + test.get().output
      runtimeClasspath += output + compileClasspath
    }
  }
}

val integrationTest = task<Test>("integrationTest") {
  description = "Runs the integration tests"
  group = "verification"
  testClassesDirs = sourceSets["integrationTest"].output.classesDirs
  classpath = sourceSets["integrationTest"].runtimeClasspath
  mustRunAfter(tasks["test"])
}

tasks.check {
  dependsOn(integrationTest)
}
  • Move all your integration tests to src/integrationTest/kotlin

And, that’s it 🙂

If unit tests fail, integration tests won’t be run. Problem solved 🙂

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *