Hello. What is the standard way to write unit test...
# kotlin-native
d
Hello. What is the standard way to write unit tests for kotlin native code? I can see that
assertEquals()
works in
main
but not sure how to run tests with CLion test runner (or gradle or cmake).
t
d
From what I can see the
CalculatorTest
runs on jvm, not native. And I’m trying to run tests in kotlin native without jvm (or multiplatform).
t
this pullrequest is exactly about running native (non-jvm) tests
g
This is how you can configure Gradle plugin to run native tests https://github.com/gildor/knel/blob/master/native/build.gradle.kts#L10-L22 just put your tests to
src/test/kotlin
dir. Also this config works with tests from common module
d
@gildor I got it working eventually, thank you. The only problem is that it seems tests can’t depend on main program without introducing a library.
t
Same here ^
Afair you can't use enableMultiplatform=true in program, only in library/framework
k
Any suggestions to testing kotlin code(crossplatform) inside Swift app (as it behave sometimes differently in ios/jvm)?
library imported as pod
t
@Konstantin Petrukhnov native-test binary should be enough I think, otherwise you have to copy-paste tests
k
@thevery any link with explanation?
t
@Konstantin Petrukhnov explanation for what?
k
"native-test binary "
n
Here https://github.com/defio/KotlinNativeExperiments I've setup the unit tests also into the common module adding
Copy code
testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version" 
testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
in the common module build.gradle file After this, to run them,
Copy code
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
into the build.gradle of the android module and
Copy code
konanArtifacts {
    
    //... STUFF ...

    program('NativeTest') {
        srcDir 'src/test/kotlin'
        commonSourceSet 'test'
        libraries {
            artifact 'NativeLib'
        }
        extraOpts '-tr'
    }
}

task test(dependsOn: 'compileKonanNativeTestIos_x64', type: Exec) {
    def textExecutable = tasks["compileKonanNativeTestIos_x64"].artifactPath
    commandLine("xcrun", "simctl", "spawn", "iPhone 8", textExecutable)
}
into the build.gradle of the iOS module. To run the tests
./gradlew :kotlinLibrary:android:test
or
./gradlew :kotlinLibrary:ios:test
😉 Here the full description: https://github.com/defio/KotlinNativeExperiments#unit-tests-in-the-common-module
t
first comment in this thread actually)
d
A bit unrelated to the thread but does anyone have Kotlin Native tests working from CLion (i.e. using IDE test runner)?