| name | choosing-test-rule-vs-runtest |
| description | 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". |
| license | Apache-2.0. See LICENSE for complete terms. |
| metadata | {"author":"Jaewoong Eum (skydoves)","keywords":["jetpack-compose","ui-testing","createComposeRule","createAndroidComposeRule","createEmptyComposeRule","runComposeUiTest","ComposeTestRule","ComposeUiTest","v2-test-api","StandardTestDispatcher","effect-context"]} |
Choosing Test Rule vs runComposeUiTest — One Entry Point Per Test
Compose ships two parallel test entry points: a JUnit4 TestRule (createComposeRule() and friends) and a multiplatform suspending lambda (runComposeUiTest { }). Each one independently sets up the recomposer, MainTestClock, and IdlingResource. Mixing them in a single test produces double-environment bugs that are hard to diagnose. This skill picks the right one and surfaces the v1 → v2 deprecation that bites every existing codebase.
When to use this skill
- The user is starting a new Compose UI test and is choosing between
createComposeRule() and runComposeUiTest { }.
- The IDE shows a deprecation
WARNING on import androidx.compose.ui.test.junit4.createComposeRule or import androidx.compose.ui.test.runComposeUiTest.
- The user reports tests passing on v1 entry points but breaking after migrating because
LaunchedEffects no longer run synchronously.
- The user has a custom
ComponentActivity subclass and is unsure between createAndroidComposeRule and createEmptyComposeRule.
- The user reports
IllegalStateException: setContent can only be called once per ComposeTestRule or "the launched Activity already calls setContent".
- The user is writing Compose Multiplatform (KMP) tests and needs the suspending entry point.
When NOT to use this skill
- The dependencies do not yet compile — start with
./configuring-test-dependencies/SKILL.md.
- The choice is host vs device, not rule vs lambda — see
./setting-up-host-vs-device-tests/SKILL.md.
- The test compiles and runs but flakes on idle/animation — see
../../synchronization/synchronizing-with-idle/SKILL.md and ../../synchronization/testing-animations-deterministically/SKILL.md.
Prerequisites
androidx.compose.ui:ui-test, androidx.compose.ui:ui-test-junit4, and (for createComposeRule() / runComposeUiTest { }) androidx.compose.ui:ui-test-manifest on the right configurations — see ./configuring-test-dependencies/SKILL.md.
- A test class with JUnit4 on the classpath if the developer chooses the rule path.
- Working knowledge that
MainTestClock and the recomposer share one kotlinx.coroutines.test.TestCoroutineScheduler.
Workflow
"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() carries an analogous but separate deprecation message (in ComposeTestRule.jvmAndAndroid.kt:347-355) pointing at androidx.compose.ui.test.junit4.v2.createComposeRule. The two messages are NOT byte-identical — quote whichever applies to the API the developer is actually migrating.
Migration mapping:
| v1 (deprecated WARNING) | v2 (recommended) |
|---|
androidx.compose.ui.test.junit4.createComposeRule | androidx.compose.ui.test.junit4.v2.createComposeRule |
androidx.compose.ui.test.junit4.createAndroidComposeRule | androidx.compose.ui.test.junit4.v2.createAndroidComposeRule |
androidx.compose.ui.test.junit4.createEmptyComposeRule | androidx.compose.ui.test.junit4.v2.createEmptyComposeRule |
androidx.compose.ui.test.runComposeUiTest | androidx.compose.ui.test.v2.runComposeUiTest |
androidx.compose.ui.test.runAndroidComposeUiTest | androidx.compose.ui.test.v2.runAndroidComposeUiTest |
androidx.compose.ui.test.runEmptyComposeUiTest | androidx.compose.ui.test.v2.runEmptyComposeUiTest |
Behavior delta: v1 uses UnconfinedTestDispatcher (eager), v2 uses StandardTestDispatcher (queued). After migration, tests that relied on a LaunchedEffect / rememberCoroutineScope block running synchronously may need an explicit mainClock.advanceTimeBy(0) or runCurrent() to drain queued work. This is the only common breaking change.
"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."
Symptoms of accidental mixing: doubled setContent calls, recompositions running on the wrong scheduler, MainTestClock advancing in one environment but not the other, waitForIdle returning instantly because it queries the wrong recomposer.
Patterns
Pattern: WRONG vs RIGHT — v1 vs v2 import
import androidx.compose.ui.test.junit4.createComposeRule
class MyTest {
@get:Rule val rule = createComposeRule()
}
import androidx.compose.ui.test.junit4.v2.createComposeRule
class MyTest {
@get:Rule val rule = createComposeRule()
}
Pattern: WRONG vs RIGHT — mixing rule and lambda
class MyTest {
@get:Rule val rule = createComposeRule()
@Test
fun bad() = runComposeUiTest {
rule.setContent { App() }
onNodeWithTag("save").performClick()
}
}
class MyTest {
@Test
fun good() = runComposeUiTest {
setContent { App() }
onNodeWithTag("save").performClick()
}
}
Pattern: WRONG vs RIGHT — createEmptyComposeRule misuse
@get:Rule val rule = createEmptyComposeRule()
@Test fun broken() {
rule.setContent { App() }
}
@get:Rule val rule = createEmptyComposeRule()
@Test fun ok() {
ActivityScenario.launch(MyActivity::class.java).use {
rule.onNodeWithTag("save").performClick()
}
}
Pattern: WRONG vs RIGHT — Activity already sets content
@get:Rule val rule = createAndroidComposeRule<MainActivity>()
@Test fun bad() {
rule.setContent { App() }
}
@get:Rule val rule = createAndroidComposeRule<MainActivity>()
@Test fun good() {
rule.onNodeWithTag("save").performClick()
}
Pattern: lambda path with custom dispatcher
@OptIn(ExperimentalTestApi::class, ExperimentalCoroutinesApi::class)
@Test
fun launchedEffectFlow() = runComposeUiTest(
effectContext = StandardTestDispatcher(),
) {
setContent { ScreenWithLaunchedEffect() }
mainClock.advanceTimeBy(0)
onNodeWithTag("counter").assertTextEquals("1")
}
Pattern: rule chain with Hilt + Compose
@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()
}
This is the canonical reason to keep the JUnit4 rule path: RuleChain ordering. The lambda path has no equivalent — the developer has to manage Hilt initialization manually inside the suspending block.
Mandatory rules
- MUST prefer v2 imports (
androidx.compose.ui.test.junit4.v2.*, androidx.compose.ui.test.v2.*) over v1 for new code. The v1 forms are @Deprecated(level = WARNING).
- MUST NOT mix
runComposeUiTest { } and a @get:Rule ComposeTestRule in the same test class. They manage independent test environments.
- MUST NOT call
setContent more than once per test. Both surfaces throw on the second call.
- MUST NOT call
composeTestRule.setContent when the launched Activity has already called setContent itself. Use createAndroidComposeRule<A>() and query the existing tree.
- MUST match the rule constructor to the host requirements:
createComposeRule() for ComponentActivity, createAndroidComposeRule<A>() for a custom Activity, createEmptyComposeRule() only when the test launches its own ActivityScenario.
- MUST declare any custom Activity used by
createAndroidComposeRule<A>() in src/androidTest/AndroidManifest.xml (or src/debug/AndroidManifest.xml) — see ./configuring-test-dependencies/SKILL.md.
- PREFERRED: when production code uses heavy
LaunchedEffects or rememberCoroutineScope, use the lambda path with effectContext = StandardTestDispatcher() so composition and the test body share one scheduler.
- PREFERRED: rely on
mainClock.advanceTimeBy(0) (test clock) rather than runOnIdle { } (wall clock + idle wait) to drain v2 queued work — see ../../synchronization/testing-animations-deterministically/SKILL.md for the autoAdvance contract.
Verification
References
- Compose testing overview (Android Developers): https://developer.android.com/develop/ui/compose/testing
- Compose testing cheat sheet: https://developer.android.com/develop/ui/compose/testing-cheatsheet
- Compose Multiplatform testing: https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-test.html
- Compose UI release notes: https://developer.android.com/jetpack/androidx/releases/compose-ui
compose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/ComposeUiTest.android.kt — runComposeUiTest, runAndroidComposeUiTest, runEmptyComposeUiTest, plus the v1 deprecation message. KDocs at lines 157-160, 204, 247, 338 forbid mixing rule + lambda.
compose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/v2/ComposeUiTest.android.kt — v2 runComposeUiTest actuals using StandardTestDispatcher.
compose/ui/ui-test-junit4/src/jvmAndAndroidMain/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule.jvmAndAndroid.kt — interface ComposeTestRule, interface ComposeContentTestRule : ComposeTestRule { fun setContent(...) }.
compose/ui/ui-test-junit4/src/androidMain/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule.android.kt — actual createComposeRule(), createAndroidComposeRule<A>(), createEmptyComposeRule().
compose/ui/ui-test-junit4/src/androidMain/kotlin/androidx/compose/ui/test/junit4/v2/AndroidComposeTestRule.android.kt — v2 actuals (StandardTestDispatcher default).