Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do Unit Testing in Kotlin/Native Multiplatform?

I have follow the tutorial of https://kotlinlang.org/docs/tutorials/native/mpp-ios-android.html and I wanted to Unit testing the commonTest I created in the SharedModule.

enter image description here

Things I tried:

I had tried using kotlin.test in the common file. I research online that you can use JUnit5 but when I imported in the dependencies in the commonTest, I can't access the library of the kotlin.test. Now I had successfully implemented the kotlin.test in the commonTest but how do I run it?

I tried gradlew commonTest.kt but it doesn't work. Please help thanks!

My SharedModule gradle codes:

    apply plugin: 'kotlin-multiplatform'

kotlin {
    targets {
        final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
                              ? presets.iosArm64 : presets.iosX64

        fromPreset(iOSTarget, 'iOS') {
            binaries {
                framework('SharedCode')
            }
        }

        fromPreset(presets.jvm, 'android')
    }

    sourceSets {
        commonMain.dependencies {
            api 'org.jetbrains.kotlin:kotlin-stdlib-common'
            api 'org.jetbrains.kotlin:kotlin-stdlib'
            //implementation("com.ionspin.kotlin:bignum:0.0.8")

            //Testing
            //implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
            //implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
/*
            implementation "io.ktor:ktor-client-core:$ktor_version"
            implementation "io.ktor:ktor-client-json:$ktor_version"
            implementation "io.ktor:ktor-client-logging:$ktor_version"*/

        }

        commonTest.dependencies{
            //implementation ("org.junit.jupiter:junit-jupiter-api:5.2.0")
            //implementation ("org.junit.jupiter:junit-jupiter-engine:5.0.3")
            //implementation 'junit:junit:4.12'
            implementation 'org.jetbrains.kotlin:kotlin-test-junit5:1.3.31'
            implementation 'org.jetbrains.kotlin:kotlin-test:1.3.31'
            //implementation "io.mockk:mockk-common:1.9.3"
            //implementation 'io.kotlintest:kotlintest-runner-junit5:3.3.0'

            //Testing
            //implementation "org.jetbrains.kotlin:kotlin-test-common"
            //implementation "org.jetbrains.kotlin:kotlin-test-annotations-common"

            //implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
            implementation "io.mockk:mockk:1.8.13.kotlin13"
            implementation "io.mockk:mockk-common:1.8.13.kotlin13"
            implementation 'org.amshove.kluent:kluent:1.42'
        }

        androidMain.dependencies {
            api 'org.jetbrains.kotlin:kotlin-stdlib'
        }
    }
}

// workaround for https://youtrack.jetbrains.com/issue/KT-27170
configurations {
    compileClasspath
}

task packForXCode(type: Sync) {
    final File frameworkDir = new File(buildDir, "xcode-frameworks")
    final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
    final def framework = kotlin.targets.iOS.binaries.getFramework("SharedCode", mode)

    inputs.property "mode", mode
    dependsOn framework.linkTask

    from { framework.outputFile.parentFile }
    into frameworkDir

    doLast {
        new File(frameworkDir, 'gradlew').with {
            text = "#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$@\n"
            setExecutable(true)
        }
    }
}
tasks.build.dependsOn packForXCode
like image 736
Cheeseburger Avatar asked May 23 '19 04:05

Cheeseburger


People also ask

How do you write a unit test on Kotlin?

Test structure Each test should be created from the following blocks: Arrange/Given - in which we will prepare all needed data required to perform test. Act/When - in which we will call single method on tested object. Assert/Then - in which we will check result of the test, either pass or fail.

How do you run a test in Kotlin?

To run tests for all targets, run the check task. To run tests for a particular target suitable for testing, run a test task <targetName>Test .


1 Answers

I was able to get this to work based off of the mpp-example git repo. My gradle file looks like:

kotlin {
    sourceSets {
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        iosTest {
            dependsOn commonTest //possibly not needed
        }
    }
}
task iosTest {
    def device = project.findProperty("iosDevice")?.toString() ?: "iPhone 8"
    dependsOn 'linkTestDebugExecutableIos'
    group = JavaBasePlugin.VERIFICATION_GROUP
    description = "Runs tests for target 'ios' on an iOS simulator"
    
    doLast {
        def binary = kotlin.targets.ios.binaries.getExecutable('test', 'DEBUG').outputFile
        exec {
            commandLine 'xcrun', 'simctl', 'spawn', device, binary.absolutePath
        }
    }
}

The main downside is that the tests have to be run in a terminal and they don't output a pretty html file afterwards. All of the results show up in the terminal.

like image 180
Sam Corder Avatar answered Sep 16 '22 19:09

Sam Corder