Search code examples
kotlintestingkotlin-multiplatformcompose-desktopandroid-jetpack-compose

Kotlin Multiplatform testing commonTest @Composable UI


I'm attempting a Kotlin Compose Multiplatform project and want to write a unit test for a piece of @Composable UI in my commonMain project. From the references I've found online, I should be able to do the following:


@get:Rule
val compose = createComposeRule()

But I haven't been able to find the right dependency to have createComposeRule show up as a valid function.

My current build.gradle.kts has this (just displaying applicable points):

plugins {
   kotlin("multiplatform") version "1.7.10"
   id("org.jetbrains.compose") version "1.3.0"
}

kotlin {
   sourceSets {
      val commonTest by getting {
         dependencies {
            implementation(kotlin("test"))
            implementation(kotlin("test-junit"))
            implementation(kotlin("test-common"))
            implementation(kotlin("test-annotations-common"))
            implementation(compose("org.jetbrains.compose.ui:ui-test-junit4"))
         }
      }
   }
}

Is there an available dependency or version I'm missing to make createComposeRule available, or should I be using a different type of UI test for this source set?


Solution

  • I upgraded my IntelliJ from a 2021 version to 2023.2.2 yesterday. This actually fixed 2 issues I was having:

    1. The above issue. I can now import and use createComposeRule() method in the commonTest source set and it runs successfully.
    2. I was having an error syntax highlight showing up on the runTest method. The IDE was being troublesome and insisting that there was some missing dependency, and the file would appear to have an error. But Gradle would execute the test task successfully. After upgrading IntelliJ, the error syntax highlighting went away.