使用 JUnit 测试 Compose Multiplatform UI
Compose Multiplatform 桌面端提供了一个基于 JUnit 和 Jetpack Compose 测试 API 的测试 API。 有关实现的更多详细信息,请参阅 Jetpack Compose 文档中的测试 Compose 布局指南。
有关所有受支持平台上可用的 UI 测试功能,请参阅测试 Compose Multiplatform UI一文。
要了解基于 JUnit 的测试如何运作,让我们从 Kotlin Multiplatform 向导生成的项目开始。 如果您是在现有项目中添加测试,则可能需要将路径和命令中的 shared 替换为您正在测试的模块名称。
创建测试源集并添加必要的依赖项:
创建一个测试目录:
shared/src/desktopTest/kotlin。在
shared/build.gradle.kts文件中,添加以下依赖项:kotlinkotlin { //... sourceSets { //... val desktopTest by getting { dependencies { implementation(compose.desktop.uiTestJUnit4) implementation(compose.desktop.currentOs) } } } }创建一个名为
ExampleTest.kt的测试文件,并将以下代码复制到其中:kotlinimport 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.v2.createComposeRule import org.junit.Rule import org.junit.Test class ExampleTest { @get:Rule val rule = createComposeRule() @Test fun myTest(){ // 声明一个模拟 UI 以演示 API 调用 // // 替换为您自己的声明,以测试项目中的代码 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") } } // 使用基于 JUnit 的测试 API 的断言和操作来测试声明的 UI rule.onNodeWithTag("text").assertTextEquals("Hello") rule.onNodeWithTag("button").performClick() rule.onNodeWithTag("text").assertTextEquals("Compose") } }要运行测试,点击
myTest()函数旁边装订区域中的运行图标,或者在终端中运行以下命令:shell./gradlew desktopTest
下一步?
- 详细了解如何创建和运行多平台测试。
- 有关 Kotlin 项目中基于 JUnit 测试的一般概述,请参阅在 JVM 中使用 JUnit 测试代码教程。