https://kotlinlang.org logo
#compose
Title
# compose
d

David Albers

11/02/2020, 9:03 PM
Is it possible to test Compose without running the test on a device/emulator?
I’ve followed the testing docs but these tests require a device (it seems). I’m wondering if it’s possible to run the tests with Robolectric or even just JVM? There are some previous threads from last year mentioning this but I don’t see any examples.
a

Adam Powell

11/02/2020, 9:30 PM
it should work fine with robolectric, we have a few robolectric tests lurking around in compose itself
compose-desktop of course runs on a local jvm, but not if you are testing things that have other android dependencies
d

David Albers

11/02/2020, 10:21 PM
If you have any of those Robolectric tests from within compose handy could you link them? I was trying to write some in Robolectric but I kept getting:
Copy code
java.lang.IllegalStateException: Functions that involve synchronization (Assertions, Actions, Synchronization; e.g. assertIsSelected(), doClick(), runOnIdle()) cannot be run from the main thread. Did you nest such a function inside runOnIdle {}, runOnUiThread {} or setContent {}?
c

Colton Idle

11/03/2020, 12:03 AM
@Adam Powell I thought each composable would be able to be tested on the jvm. Maybe I misinterpreted something from months ago. I don't think it's the worst thing if I need to test my composables on device. Anything that gets me away from robolectric because that always ends up being a dumpster fire.
a

Adam Powell

11/03/2020, 12:14 AM
If you write a composable function like
Copy code
ContextAmbient.current.getSystemService(...)...
then that's a platform-specific composable 🙂
if you have composables that are platform-agnostic, then those will work host-side just fine
1
m

Mini

12/17/2020, 7:28 PM
@David Albers did you get robolectric tests up and running?
d

David Albers

12/21/2020, 8:34 PM
Hey @Mini, I was able to test Composables if I used this runner or upgraded to Robolectric 4.4. However, there are some view-testing function such as
assertIsDisplayed()
(this) that I never figured out how to get working in Robolectric.
1
🙌 1
a

allan.conda

12/23/2020, 4:21 PM
Did you create your own copy of the runner?
d

David Albers

01/05/2021, 3:39 PM
Hi Allan. Right, I copied the
ComposeUiRobolectricTestRunner
into my project. Then I set it as the runner for Robolectric tests like so:
Copy code
@RunWith(ComposeUiRobolectricTestRunner::class)
class YourComposableTest {
....
hope that helps!