Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions topics/compose/compose-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ to replace `composeApp` in paths and commands with the module name you are testi
Create a common test source set and add the necessary dependencies:

1. Create a directory for the common test source set: `composeApp/src/commonTest/kotlin`.
2. In the `composeApp/build.gradle.kts` file, add the following dependencies:
2. In the `composeApp/build.gradle.kts` file, add the following configuration:

```kotlin
kotlin {
//...
sourceSets {
val desktopTest by getting
val jvmTest by getting

// Adds common test dependencies
commonTest.dependencies {
Expand All @@ -47,7 +47,7 @@ Create a common test source set and add the necessary dependencies:
}

// Adds the desktop test dependency
desktopTest.dependencies {
jvmTest.dependencies {
implementation(compose.desktop.currentOs)
}
}
Expand All @@ -56,7 +56,7 @@ Create a common test source set and add the necessary dependencies:

3. If you need to run instrumented (emulator) tests for Android, amend your Gradle configuration as follows:
1. Add the following code to the `androidTarget {}` block to configure the instrumented test source set to depend on
a common test source set. Then, follow the IDE's suggestions to add any missing imports.
a common test source set.

```kotlin
kotlin {
Expand All @@ -69,8 +69,8 @@ Create a common test source set and add the necessary dependencies:
//...
}
```

2. Add the following code to the `android.defaultConfig {}` block to configure the Android test instrumentation runner:
2. Follow the IDE's suggestions to add any missing imports.
3. Add the following code to the `android.defaultConfig {}` block to configure the Android test instrumentation runner:

```kotlin
android {
Expand All @@ -82,20 +82,16 @@ Create a common test source set and add the necessary dependencies:
}
```

3. Add the required dependencies for `androidTarget`:
4. Add the required dependencies in the root `dependencies {}` block:

```kotlin
kotlin {
// ...
androidTarget {
// ...
dependencies {
androidTestImplementation("androidx.compose.ui:ui-test-junit4-android:%androidx.compose%")
debugImplementation("androidx.compose.ui:ui-test-manifest:%androidx.compose%")
}
}
}
dependencies {
androidTestImplementation("androidx.compose.ui:ui-test-junit4-android:%androidx.compose%")
debugImplementation("androidx.compose.ui:ui-test-manifest:%androidx.compose%")
}
```
4. Select **Build | Sync Project with Gradle Files** in the main menu, or click the Gradle refresh button in the
build script editor.

Now, you are ready to write and run common tests for the Compose Multiplatform UI.

Expand All @@ -104,11 +100,19 @@ Now, you are ready to write and run common tests for the Compose Multiplatform U
In the `composeApp/src/commonTest/kotlin` directory, create a file named `ExampleTest.kt` and copy the following code into it:

```kotlin
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.*
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.runComposeUiTest
import kotlin.test.Test

class ExampleTest {
Expand Down Expand Up @@ -148,7 +152,7 @@ To run tests:

You have two options:
* In Android Studio, you can click the green run icon in the gutter next to the `myTest()` function, choose
**Run** and the iOS target for the test.
**Run | ExampleTest.myTest**, then select the iOS target for the test.
* Run the following command in the terminal:

```shell
Expand All @@ -171,11 +175,11 @@ Android Studio, for example, won't be helpful.
<tab title="Desktop">

You have two options:
* Click the green run icon in the gutter next to the `myTest()` function and choose **Run&nbsp;|&nbsp;desktop**.
* Click the green run icon in the gutter next to the `myTest()` function and choose **Run | ExampleTest.myTest**, then select the JVM target.
* Run the following command in the terminal:

```shell
./gradlew :composeApp:desktopTest
./gradlew :composeApp:jvmTest
```

</tab>
Expand Down