Bob Dudan
Bob Dudan

Reputation: 97

How to create separate tests with Gradle and Kotlin?

I am working on a Kotlin project with Gradle that includes unit tests. I want to add some integration tests (or functional tests, never understood the difference between the two), but I want to be able to run them independently. Ideally, the source of the tests are in different folders.

I am using Gradle 4.5 and my build.gradle file looks something like this :

buildscript {
    ext.kotlin_version = '1.2.21'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin'
apply plugin: 'application'

mainClassName = 'xyz.bobdudan.myproject.AppKt'

repositories {
    maven { url "http://maven.stardog.com" }
    mavenCentral()
}
dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    testCompile 'io.kotlintest:kotlintest:2.0.7'
}

I have tried the method described here for java, but it doesn't work : the task also runs the unit tests, but they can't be found, and the integration tests are not executed at all.

What can i do ?

Edit :

Here is the result of gradle clean integTest with the solution of @lance-java:

:clean
:compileIntegTestKotlin
:compileIntegTestJava NO-SOURCE
:processIntegTestResources NO-SOURCE
:integTestClasses UP-TO-DATE
:integTest NO-SOURCE

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed

So nothing is executed (I make sure that the tests are supposed to fail)

Upvotes: 1

Views: 3539

Answers (2)

Клаус Шварц
Клаус Шварц

Reputation: 3268

To tell long story short you have to:

  1. create separate source sets,
  2. create testTask for each test source,
  3. set it up,
  4. define order these tasks should be executed during build/check

and you are good to go.

Here is a guide in an answer to another question about how to do this for project using JUnit5 and Kotlin Spek test framework.

Upvotes: 1

lance-java
lance-java

Reputation: 27966

Note: I'm not a kotlin dev so will write in groovy.

You can add another SourceSet which will automatically add a JavaCompile task for the SourceSet and also a few Configuration instances to the model (eg integTestCompile, integTestCompileOnly, integTestRuntime etc)

sourceSets {
    integTest {
        java.srcDir 'src/integTest/java'
        resources.srcDir 'src/integTest/resources'
    }
}
configurations {
    integTestCompile.extendsFrom compile
}

Then you can add another Test task

task integTest(type: Test) {  
    testClassesDir = sourceSets.integTest.output.classesDir
    classpath = sourceSets.integTest.runtimeClasspath
}

You may wish to wire the integTest task into the normal DAG, or perhaps you'll leave this off and call it explicitly

check.dependsOn integTest  

Upvotes: 1

Related Questions