Jetpack Compose UI测试在设置时中断



我正在Jetpack Compose应用程序上尝试一个相当简单的UI测试。

SUT是一个登录屏幕,包含两个用于电子邮件和密码的字段以及一个用于密码重置的链接。

当点击重置链接时,密码字段将隐藏,电子邮件字段将保留。

以下是整个测试类:

@HiltAndroidTest
class LoginScreenTests {
@get:Rule(order = 0)
val hiltRule = HiltAndroidRule(this)
@get:Rule(order = 1)
val composeRule = createAndroidComposeRule<MainActivity>()
@Before
fun setup() {
hiltRule.inject()
composeRule.setContent {
MyTheme {
LoginScreen()
}
}
}
@Test
fun onStart_shouldDisplayEmailAndPass() {
composeRule.onNodeWithTag("email").assertIsDisplayed()
composeRule.onNodeWithTag("password").assertIsDisplayed()
}
@Test
fun resetMode_shouldHidePass() {
composeRule.onNodeWithTag("resetPassword").performClick()
composeRule.onNodeWithTag("email").assertIsDisplayed()
composeRule.onNodeWithTag("password").assertDoesNotExist()
}
}

当我单独运行它们时,两个测试都通过。当我运行类时,第二个测试失败。

正如我所注意到的,甚至在测试函数中也没有发生故障。它中断了设置功能,但我无法确定中断的原因。我可以在hiltRule.injectcomposeRule.setContent上设置断点,断点确实命中,但即使被断点停止,进程也会被终止。

还有一个失败测试的日志,上面写着关于注销输入通道的内容。

02-13 20:39:39.475 26380 26412 I TestRunner: started: onStart_shouldDisplayEmailAndPass(com.sample.ui.screens.LoginScreenTests)
02-13 20:39:39.488 26380 26412 W Settings: Setting always_finish_activities has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
02-13 20:39:39.518 10516 10534 D EventSequenceValidator: inc AccIntentStartedEvents to 26
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.535 10516 15024 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.535 10516 15024 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.535 10516 15024 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.584   304  8900 D goldfish-address-space: claimShared: Ask to claim region [0x3f6ed4000 0x3f76bd000]
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.658 10516 11864 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.660 10516 11864 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.660 10516 11864 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.660 10516 11864 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.660 10516 11864 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.676   304  8900 D goldfish-address-space: claimShared: Ask to claim region [0x3f28f2000 0x3f30db000]
02-13 20:39:39.687 11465 26026 I PBSessionCacheImpl: Deleted sessionId[50425247988668063] from persistence.
02-13 20:39:39.696 11465 12035 W SearchServiceCore: Abort, client detached.
02-13 20:39:39.702 26380 26380 D LifecycleMonitor: Lifecycle status change: com.sample.MainActivity@6689b60 in: PRE_ON_CREATE
02-13 20:39:39.702 26380 26380 V ActivityScenario: Activity lifecycle changed event received but ignored because the reported transition was not ON_CREATE while the last known transition was PRE_ON_CREATE
02-13 20:39:39.704 26380 26410 V FA      : onActivityCreated
02-13 20:39:39.710 26380 26380 D LifecycleMonitor: Lifecycle status change: com.sample.MainActivity@6689b60 in: CREATED
02-13 20:39:39.710 26380 26380 V ActivityScenario: Update currentActivityStage to CREATED, currentActivity=com.sample.MainActivity@6689b60
02-13 20:39:39.716 26380 26380 D LifecycleMonitor: Lifecycle status change: com.sample.MainActivity@6689b60 in: STARTED
02-13 20:39:39.716 26380 26380 V ActivityScenario: Update currentActivityStage to STARTED, currentActivity=com.sample.MainActivity@6689b60
02-13 20:39:39.719 26380 26414 V FA      : Activity resumed, time: 59230451
02-13 20:39:39.720 26380 26380 D LifecycleMonitor: Lifecycle status change: com.sample.MainActivity@6689b60 in: RESUMED
02-13 20:39:39.721 26380 26380 V ActivityScenario: Update currentActivityStage to RESUMED, currentActivity=com.sample.MainActivity@6689b60
02-13 20:39:39.739 26380 26380 D LandingScreen.kt: is tablet: false
02-13 20:39:39.802 26380 26460 I Process : Sending signal. PID: 26380 SIG: 9
02-13 20:39:39.847 10516 12816 W InputDispatcher: Attempted to unregister already unregistered input channel 'bd87344 com.sample/com.sample.MainActivity (server)'
02-13 20:39:39.855 11520 11520 D BoundBrokerSvc: onUnbind: Intent { act=com.google.android.gms.measurement.START pkg=com.google.android.gms }
02-13 20:39:39.865 26366 26366 D AndroidRuntime: Shutting down VM
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.869 10516 14101 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.882 10477 10477 I Zygote  : Process 26380 exited due to signal 9 (Killed)

您需要在类中添加此行

@RunWith(AndroidJUnit4::class)

并且这些行对于代码来说是不必要的

@HiltAndroidTest
@get:Rule(order = 0)
val hiltRule = HiltAndroidRule(this)
hiltRule.inject()

您的代码应该看起来像

@RunWith(AndroidJUnit4::class)
class LoginScreenTests {

@get:Rule
val composeRule = createAndroidComposeRule<MainActivity>()
@Before
fun setup() {
composeRule.setContent {
MyTheme {
LoginScreen()
}
}
}
@Test
fun onStart_shouldDisplayEmailAndPass() {
composeRule.onNodeWithTag("email").assertIsDisplayed()
composeRule.onNodeWithTag("password").assertIsDisplayed()
}
@Test
fun resetMode_shouldHidePass() {
composeRule.onNodeWithTag("resetPassword").performClick()
composeRule.onNodeWithTag("email").assertIsDisplayed()
composeRule.onNodeWithTag("password").assertDoesNotExist()
}
}

最新更新