I want to test composables without the need for an...
# compose
m
I want to test composables without the need for an emulator/device. Theres been talk in this channel of using robolectric, with custom runners etc. and people have reported partial success, however I have not been able to set it up properly. I have also tried to add the compose-desktop version of the compose testing library to my android project, also with no success. Could anyone shed some light on what would be the best way to go about running compose tests on JVM?
j
I think using Compose for Desktop would be your best bet, but it is kinda self-service at the moment, as we don't have an official set of test rules at the moment. Such a thing will likely come at some point after Compose 1.0 is released.
We have a bunch of unit tests in the androidx library (see: https://cs.android.com/search?q=DesktopScreenshotTestRule). It would likely take some effort to extract the relevant pieces for your own usage though.
Until such time as a more official solution is released.
💯 1
m
Jim, I have been playing around with this for a while now, but I dont seem to be getting anywhere. Honestly I think my understanding of multiplatform is quite lacking. Ideally I would just add testImplementation org.jetbrains.compose.ui:ui-test-junit4 to my android project and run composable tests that do not require anything android from my test folder. However that dependency seems to provide the aar i would get from an androidx dependency (assuming this is simply a proxy for multiplatform projects??) Then I tried extracting parts of the DesktopComposeTestRule but realised I would basically be reimplementing compose for desktop, which seems silly. I figured another option would be to create a jvm library that depends on the jetbrains version and then exposes the rule I need to the rest of my project. So, I am pretty much just guessing at where to start, and failing miserably 😅 Could you elaborate on what you mean by extract the relevant pieces, and point me in the right direction? 🙏
j
I figured another option would be to create a jvm library that depends on the jetbrains version and then exposes the rule
When I said "using Compose for Desktop would be your best bet", that does mean taking a dependency on the jetbrains version. You may want to look at the work @cb did with Accompanist to see how he introduced tests using desktop: • Tweet: https://twitter.com/chrisbanes/status/1390653509146660864 • Source code: https://github.com/google/accompanist/pull/393
👀 2
m
Well, seems like he has done exactly what I want.. 🤔 Thank you Jim!
👍 1
f
hey @jim - is there any update on this topic? I was now looking into the possibility of running compose tests without the resort to the emulator.
j
I don't think there is any news here yet. cc @Jelle Fresen [G]
🙏 1
j
Correct, no news yet
m
@fabio.carballo you can however use robolectric now
f
hmm - really @Mini? do you have any sample on the web or so? yesterday I had tried to run it on top of
FragmentScenario
with no success.
m
I do something like this:
Copy code
class TestApplication : Application()

@RunWith(AndroidJUnit4::class)
@Config(application = TestApplication::class)
class SomeTest {

    @get:Rule
    val rule = createComposeRule()
    
    @Test
    fun someTest() {

        rule.setContent { 
            Text("Hello")
        }

        rule.onNodeWithText("Hello").assertIsDisplayed()
    }
}
🙏 1
j
Yes, this should work. A few things to keep in mind: nothing is rendered (so no captureToImage()); no access to Bitmap (so no ImageBitmap); text is measured with default values (so can't assert size of components that wrap around text); and custom test runners are not yet supported
AFAIA these are mostly the same limitations as non-compose Robolectric tests
f
okok, perfect! thanks for the clarifications