Tim Malseed
12/01/2021, 9:16 PMcomposeTestRule.setContent { MyScreen() }
- if you’ve written Compose UI tests, you’d be familiar with this.
If you need to run the tests against a particular activity, for some reason, you can use androidComposeTestRule<MyActivity>
In my case, MyActivity
calls setContent { MyScreen() }
during onCreate()
.
So, what ends up happening, is the test instantiates MyActivity
, which calls setContent()
, and the screen is composed, with its ViewModel and various dependencies. While this is happening, the test calls its own setContent()
and the screen is composed again, in parallel! This causes all kinds of nightmarish problems.
tl;dr
Make sure when testing Compose, that you only call setContent()
once per test. And be careful about which Activity
you use to host the test, if you need one!Colton Idle
12/01/2021, 10:59 PMTim Malseed
12/01/2021, 11:01 PMandroidComposeTestRule
, and so runs inside an Activity that already calls setContent()
, then calling setContent()
again in your test can cause issues.setContent()
in test’. But rather, don’t test against an Activity that calls setContent()
My solution was to create another Activity just for test purposes. And, to keep this from prod, I only declare this Activity in the debug AndroidManifestAlex Vanyo
12/02/2021, 1:41 AMActivity
, you can use a plain createComposeRule()
instead along with the ui-test-manifest
dependency. If you add it with debugImplementation
, this should function exactly like your test activity:
debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")
Tim Malseed
12/02/2021, 1:41 AMColton Idle
12/02/2021, 2:15 AMTim Malseed
12/02/2021, 2:17 AMsetContent()
Colton Idle
12/02/2021, 2:22 AM