Kotlin Multiplatform Testing Part 2
Where he tried to run tests in androidTest and found that had been deprecated...
So next I wanted to get testing sourceSets together for desktop and android (I need a mac before I will bother with the iOs specific tests).
Adding the following directory:
commonApp/src/desktopTest/kotlin/
and adding a KotlinDesktopTest.kt to it with the same basic test:
import kotlin.test.Test
import kotlin.test.assertTrue
import kotlin.test.fail
class KotlinDesktopTest {
@Test
fun `a kotlin-test assertion`() {
assertTrue(true)
fail("whoops")
}
}
and running all tests in desktopTest resulted in a failed test, that passed when I deleted the fail(“whoops”). Thats good! Lets try it with android. All my searching said android tests went in androidTest so lets try that…
commonApp/src/androidTest/kotlin/
Well that isn’t even recognized. A little searching and I find out that it is deprecated, now you need to specify androidUnitTest and androidInstrumentedTest.
So if I make the androidUnitTest directory and run it, everything works fine (a little digging say that sourceSet automatically gets all the includes of commonTest).
Lets try the androidInstrumentedTest sourceSet which should run the tests on the android emulator:
Uh-Oh! My kotlin test doesn’t work because I don’t have access to the imports. Adding
val androidInstrumentedTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
to the sourceSets in the build.gradle.kts and removing all spaces from the test name (not allowed for instrumented tests apparently) and now I get it compiling, but when I run I get 0 tests run. Why is that? We need a special test runner. Changing the androidInstrumentedTest dependencies to this:
val androidInstrumentedTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(libs.androidx.test.junit)
implementation(libs.androidx.test.runner)
}
}
and the android target in the same /composeApp/build.gradle.kts
file like this:
android {
...
defaultConfig {
...
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
which requires an update to the /gradle/libs.versions.toml
file:
[versions]
...
androidx-test-runner = "1.5.2"
[libraries]
...
androidx-test-runner = { group = "androidx.test", name = "runner", version.ref = "androidx-test-runner"}
don’t forget to sync, now running the test gets us what we need - a failing test!
So now I am at the point where I can start developing. This went much faster this time (probably the 4th or 5th attempt…). Both Kent Beck and jbrains gave me variations of the advice: if you are trying something and it isn’t working, throw it out and try again. Great advice!
Next up is where we start actually making an app and the heroes run into yet another problem in the form of “not all libraries can be used”. So stay tuned! Also, I think I shall now put a summary of what had to be done so that this doesn’t turn into one of those recipe web pages where you get frustrated hunting around for the actual recipe instead of the long story that only the writer cares about…