Igor Demin
c44601a1f4
Crashes, because tutorial checker uses desktop-template without uiTesting module See https://teamcity.jetbrains.com/buildConfiguration/JetBrainsPublicProjects_Compose_Publish_2_AllGitHubRelease/4301157?hideProblemsFromDependencies=false&hideTestsFromDependencies=false&expandBuildProblemsSection=true&expandBuildChangesSection=true Also adding "``` kotlin` hach to avoid compilation of some snippets 9we use it in another places) |
1 year ago | |
---|---|---|
.. | ||
README.md | Fix Error in snippet at /mnt/agent/work/b8625f0804e53334/tutorials/./UI_Testing/README.md:9 (#3628) | 1 year ago |
README.md
UI Testing
The API for unit testing in Compose for Desktop is nearly identical to the Jetpack Compose Android testing API. We highly recommended reading that first before moving on to this tutorial.
Setting up
To start using the testing API, you will need to add the dependency on compose.uiTestJUnit4
to your build.gradle
file and create the directory for your tests.
If the module is desktop-only (kotlin("jvm")
is applied), add the dependency via:
dependencies {
testImplementation(compose.desktop.uiTestJUnit4)
testImplementation(compose.desktop.currentOs)
}
and the directory for tests will be src/test/kotlin
If the module is multiplatform (kotlin(“multiplatform”)
is applied), add it via:
kotlin {
sourceSets {
val desktopTest by getting {
dependencies {
implementation(compose.desktop.uiTestJUnit4)
implementation(compose.desktop.currentOs)
}
}
}
}
And the directory for tests will be src/desktopTest/kotlin
Creating your first test
In the tests directory, create a file named ExampleTest.kt
and paste this code into it:
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.*
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.junit4.createComposeRule
import org.junit.Rule
import org.junit.Test
class ExampleTest {
@get:Rule
val rule = createComposeRule()
@Test
fun myTest(){
rule.setContent {
var text by remember { mutableStateOf("Hello") }
Text(
text = text,
modifier = Modifier.testTag("text")
)
Button(
onClick = { text = "Compose" },
modifier = Modifier.testTag("button")
){
Text("Click me")
}
}
rule.onNodeWithTag("text").assertTextEquals("Hello")
rule.onNodeWithTag("button").performClick()
rule.onNodeWithTag("text").assertTextEquals("Compose")
}
}
Now you can run the test by either clicking button in your IDE, or from the command line with
./gradlew test