Loading...
Loading...
Use this skill to pick the correct Compose UI test entry point. Compares `createComposeRule()`, `createAndroidComposeRule<A>()`, `createEmptyComposeRule()`, `runComposeUiTest { }`, and `runAndroidComposeUiTest<A> { }`, plus the v1 vs v2 split (`UnconfinedTestDispatcher` vs `StandardTestDispatcher`). Encodes the rule that mixing `runComposeUiTest { }` and a `ComposeTestRule` in the same test is forbidden, that `setContent` may only run once, and that `createEmptyComposeRule()` returns `ComposeTestRule` (no `setContent`). Use when the user asks "ComposeTestRule vs ComposeUiTest", reports `IllegalStateException: setContent can only be called once`, mentions `runComposeUiTest`, the v2 deprecation warning, `effectContext`, custom `ComponentActivity`, or "host tests need a coroutine scope".
npx skill4agent add skydoves/android-testing-skills choosing-test-rule-vs-runtestTestRulecreateComposeRule()runComposeUiTest { }MainTestClockIdlingResourcecreateComposeRule()runComposeUiTest { }WARNINGimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.runComposeUiTestLaunchedEffectComponentActivitycreateAndroidComposeRulecreateEmptyComposeRuleIllegalStateException: setContent can only be called once per ComposeTestRule./configuring-test-dependencies/SKILL.md./setting-up-host-vs-device-tests/SKILL.md../../synchronization/synchronizing-with-idle/SKILL.md../../synchronization/testing-animations-deterministically/SKILL.mdandroidx.compose.ui:ui-testandroidx.compose.ui:ui-test-junit4createComposeRule()runComposeUiTest { }androidx.compose.ui:ui-test-manifest./configuring-test-dependencies/SKILL.mdMainTestClockkotlinx.coroutines.test.TestCoroutineScheduler| Path | Choose when |
|---|---|
| The codebase already uses JUnit4 rules; the developer wants one rule chained with |
| Compose Multiplatform; the developer wants |
MainTestClock| Constructor | Returns | Hosts | |
|---|---|---|---|
| | | Yes |
| | Custom Activity | Yes |
| | None — developer launches their own scenario | No |
compose/ui/ui-test-junit4/src/jvmAndAndroidMain/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule.jvmAndAndroid.ktinterface ComposeContentTestRule : ComposeTestRule { fun setContent(...) }createEmptyComposeRule()| Function | Receiver | Hosts |
|---|---|---|
| | |
| | Custom Activity |
| | None — |
runComposeUiTestrunAndroidComposeUiTesteffectContext: CoroutineContext = EmptyCoroutineContextrunTestContext: CoroutineContext = EmptyCoroutineContexttestTimeout: Duration = 60.secondsrunEmptyComposeUiTestblock: ComposeUiTest.() -> UnitActivityScenariocompose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/ComposeUiTest.android.kt:184, 232, 352…/v2/ComposeUiTest.android.kt:75, 122, 238androidx.compose.ui.test.junit4.createComposeRuleandroidx.compose.ui.test.runComposeUiTest@Deprecated(level = DeprecationLevel.WARNING)runComposeUiTestComposeUiTest.android.kt"Use `androidx.compose.ui.test.v2.runComposeUiTest` instead. The v2 APIs align with
standard coroutine behavior by queuing tasks rather than executing them
immediately. Tests relying on immediate execution may require explicit
synchronization. Please refer to the migration guide for more details."createComposeRule()ComposeTestRule.jvmAndAndroid.kt:347-355androidx.compose.ui.test.junit4.v2.createComposeRule| v1 (deprecated WARNING) | v2 (recommended) |
|---|---|
| |
| |
| |
| |
| |
| |
UnconfinedTestDispatcherStandardTestDispatcherLaunchedEffectrememberCoroutineScopemainClock.advanceTimeBy(0)runCurrent()runComposeUiTestComposeUiTest.android.kt"Keeping a reference to the [ComposeUiTest] outside of this function is an error. Also avoid
using [androidx.compose.ui.test.junit4.ComposeTestRule] (e.g., createComposeRule) inside
[runComposeUiTest][block] or any of their respective variants. Since these APIs independently
manage the test environment, mixing them may lead to unexpected behavior."setContentMainTestClockwaitForIdlesetContentComposeContentTestRule.setContentComposeUiTest.setContentIllegalStateExceptionsetContentsetContentcomposeTestRule.setContentcreateAndroidComposeRule<MyActivity>()ComposeUiTest.android.ktsetContenteffectContextCoroutineContextLaunchedEffectrememberCoroutineScopeComposeUiTest.android.ktTestDispatcherLaunchedEffecttestTimeouttestTimeout: Duration = 60.secondsAndroidComposeUiTestTimeoutException// WRONG — emits @Deprecated WARNING and uses UnconfinedTestDispatcher
import androidx.compose.ui.test.junit4.createComposeRule
class MyTest {
@get:Rule val rule = createComposeRule()
}
// WRONG because: v1 dispatches LaunchedEffect work eagerly. After bumping
// kotlinx-coroutines-test the same test will produce different observable
// behavior than runTest { } in the rest of the codebase.// RIGHT — v2, StandardTestDispatcher, no deprecation warning
import androidx.compose.ui.test.junit4.v2.createComposeRule
class MyTest {
@get:Rule val rule = createComposeRule()
}// WRONG
class MyTest {
@get:Rule val rule = createComposeRule() // env #1
@Test
fun bad() = runComposeUiTest { // env #2 — independent recomposer + clock
rule.setContent { App() } // sets content in env #1
onNodeWithTag("save").performClick() // queries env #2 — finds nothing
}
}
// WRONG because: each environment owns its own test scheduler and idling resource.
// The KDoc forbids this configuration explicitly.// RIGHT — pick one
class MyTest {
@Test
fun good() = runComposeUiTest {
setContent { App() }
onNodeWithTag("save").performClick()
}
}createEmptyComposeRule// WRONG
@get:Rule val rule = createEmptyComposeRule()
@Test fun broken() {
rule.setContent { App() } // compile error — ComposeTestRule has no setContent
}
// WRONG because: createEmptyComposeRule() returns ComposeTestRule, not ComposeContentTestRule.
// It is for tests that launch their own ActivityScenario.// RIGHT — launch your own scenario, then query
@get:Rule val rule = createEmptyComposeRule()
@Test fun ok() {
ActivityScenario.launch(MyActivity::class.java).use {
// MyActivity.onCreate calls setContent { App() }
rule.onNodeWithTag("save").performClick()
}
}// WRONG
@get:Rule val rule = createAndroidComposeRule<MainActivity>()
@Test fun bad() {
rule.setContent { App() } // throws IllegalStateException — MainActivity.onCreate already set content
}// RIGHT
@get:Rule val rule = createAndroidComposeRule<MainActivity>()
@Test fun good() {
rule.onNodeWithTag("save").performClick() // query content the Activity already installed
}@OptIn(ExperimentalTestApi::class, ExperimentalCoroutinesApi::class)
@Test
fun launchedEffectFlow() = runComposeUiTest(
effectContext = StandardTestDispatcher(), // share scheduler with composition
) {
setContent { ScreenWithLaunchedEffect() }
mainClock.advanceTimeBy(0) // drain queued LaunchedEffect work
onNodeWithTag("counter").assertTextEquals("1")
}@get:Rule(order = 0) val hilt = HiltAndroidRule(this)
@get:Rule(order = 1) val compose = createAndroidComposeRule<HiltTestActivity>()
@Test fun feed() {
hilt.inject()
compose.onNodeWithTag("feed_list").assertIsDisplayed()
}RuleChainandroidx.compose.ui.test.junit4.v2.*androidx.compose.ui.test.v2.*@Deprecated(level = WARNING)runComposeUiTest { }@get:RuleComposeTestRulesetContentcomposeTestRule.setContentsetContentcreateAndroidComposeRule<A>()createComposeRule()ComponentActivitycreateAndroidComposeRule<A>()createEmptyComposeRule()ActivityScenariocreateAndroidComposeRule<A>()src/androidTest/AndroidManifest.xmlsrc/debug/AndroidManifest.xml./configuring-test-dependencies/SKILL.mdLaunchedEffectrememberCoroutineScopeeffectContext = StandardTestDispatcher()mainClock.advanceTimeBy(0)runOnIdle { }../../synchronization/testing-animations-deterministically/SKILL.mdimport androidx.compose.ui.test.junit4.createComposeRule@DeprecatedWARNING@get:RuleComposeTestRulerunComposeUiTest { }setContentcreateEmptyComposeRule()ActivityScenarioeffectContext@OptIn(ExperimentalTestApi::class)mainClock.advanceTimeBy(0)compose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/ComposeUiTest.android.ktrunComposeUiTestrunAndroidComposeUiTestrunEmptyComposeUiTestcompose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/v2/ComposeUiTest.android.ktrunComposeUiTestStandardTestDispatchercompose/ui/ui-test-junit4/src/jvmAndAndroidMain/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule.jvmAndAndroid.ktinterface ComposeTestRuleinterface ComposeContentTestRule : ComposeTestRule { fun setContent(...) }compose/ui/ui-test-junit4/src/androidMain/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule.android.ktcreateComposeRule()createAndroidComposeRule<A>()createEmptyComposeRule()compose/ui/ui-test-junit4/src/androidMain/kotlin/androidx/compose/ui/test/junit4/v2/AndroidComposeTestRule.android.kt