Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run kotlintest tests with gradle?

The kotlintest tests run perfectly fine when started from Intellij, but when I try to run them with the gradle test task command, only my regular JUnit tests are found and run.

The kotlintest code:

import io.kotlintest.matchers.shouldBe
import io.kotlintest.specs.StringSpec

class HelloKotlinTest : StringSpec() {
    init {
        println("Start Kotlin UnitTest")

        "length should return size of string" {
            "hello".length shouldBe 5
        }
    }
}

build.gradle:

apply plugin: 'org.junit.platform.gradle.plugin'

buildscript {
    ext.kotlinVersion = '1.1.3'
    ext.junitPlatformVersion = '1.0.0-M4'

    repositories {
        maven { url 'http://nexus.acompany.ch/content/groups/public' }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath "org.junit.platform:junit-platform-gradle-plugin:$junitPlatformVersion"
    }
}

sourceSets {
    main.kotlin.srcDirs += 'src/main/kotlin'
    test.kotlin.srcDirs += 'test/main/kotlin'
}

(...) 

dependencies {
    // Kotlin
    compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jre8', version: kotlinVersion

    // Kotlin Test
    testCompile group: 'io.kotlintest', name: 'kotlintest', version: kotlinTestVersion

    // JUnit 5
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitJupiterVersion
    testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitJupiterVersion
}
like image 955
Fabian Avatar asked Jul 21 '17 07:07

Fabian


People also ask

How do I run test cases in Gradle?

Run Gradle testsIn your Gradle project, in the editor, create or select a test to run. From the context menu, select Run <test name>. icon in the left gutter. If you selected the Choose per test option, IntelliJ IDEA displays both Gradle and JUnit test runners for each test in the editor.

Does Gradle build also run tests?

By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.

How do you run a parallel test in Gradle?

By using the --parallel switch, you can force Gradle to execute tasks in parallel as long as those tasks are in different projects. You could see big improvements in build times as soon as you enable parallel builds.

How do I run a single unit test class using Gradle?

In Gradle, we can pass an --tests option to run a single unit test class.


2 Answers

In KotlinTest 3.1.x you don't need to use Junit4 anymore. It is fully compatible with JUnit 5. Thus the answer to your question is to upgrade to the 3.1.x track.

You need to add useJUnitPlatform() to the test block in your build.gradle.

You need to add testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.9' to your dependencies.

For example.

dependencies {
    testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.9'
}

test {
    useJUnitPlatform()

    // show standard out and standard error of the test JVM(s) on the console
    testLogging.showStandardStreams = true

    testLogging {
        events "PASSED", "FAILED", "SKIPPED", "STANDARD_OUT", "STANDARD_ERROR"
    }
}
like image 145
sksamuel Avatar answered Oct 21 '22 17:10

sksamuel


The "solution" was to switch back to JUnit 4.

kotlintest was not build with JUnit 5 in mind and does not provide its own junit-engine.

(Note: It should be possible to tell JUnit 5 to use the JUnit 4 engine for kotlintest. If anybody knows how to do this, please add the solution here.)

like image 23
Fabian Avatar answered Oct 21 '22 17:10

Fabian