<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jeprubio on Medium]]></title>
        <description><![CDATA[Stories by Jeprubio on Medium]]></description>
        <link>https://medium.com/@jeprubio?source=rss-7d08a2ab9c69------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*GBgJQnuT4wbMcL-g</url>
            <title>Stories by Jeprubio on Medium</title>
            <link>https://medium.com/@jeprubio?source=rss-7d08a2ab9c69------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 18:53:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jeprubio/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Testing Compose Screens with robot testing pattern]]></title>
            <link>https://medium.com/@jeprubio/testing-compose-screens-with-robot-testing-pattern-a49ea9efdfff?source=rss-7d08a2ab9c69------2</link>
            <guid isPermaLink="false">https://medium.com/p/a49ea9efdfff</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[robot-pattern]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Jeprubio]]></dc:creator>
            <pubDate>Thu, 22 Feb 2024 22:32:13 GMT</pubDate>
            <atom:updated>2024-02-23T22:58:41.636Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*bEmUGq2HD63JYAiIjFosvw.jpeg" /><figcaption>AI generated image</figcaption></figure><p>I’ve recently came across the following article <a href="https://tomasrepcik.dev/blog/2024/2024-02-06-robot-pattern-compose-testing/">https://tomasrepcik.dev/blog/2024/2024-02-06-robot-pattern-compose-testing/</a> which is a good article about using robot pattern for e2e (end to end) testing in compose.</p><p>Few days before that I was rewatching the great <em>Testing robots</em> presentation by Jake Wharton (<a href="https://jakewharton.com/testing-robots/">https://jakewharton.com/testing-robots/</a>) and thinking how could I apply that not only to e2e tests but also to <a href="https://developer.android.com/jetpack/compose/testing">compose tests</a> about specific composables, like a screen content.</p><h3><strong>Why</strong></h3><p>Using robots provides some architecture for the tests as we do with the rest of our code. Like what we do when we use presentation patterns to separate the <em>what</em> (model) from the <em>how</em> (presenters) in robot pattern the <em>test</em> should describe the <strong>WHAT</strong> and the <em>robot</em> the <strong>HOW</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*XEAb_vz516sGKeLP8jNWIQ.png" /><figcaption>Image from the presentation in <a href="https://jakewharton.com/testing-robots/">https://jakewharton.com/testing-robots/</a></figcaption></figure><p>I highly recommend watching Jake Wharton’s presentation, where he explains a concept in a manner that I find difficult to articulate myself. The presentation includes code examples demonstrating the usage of test robots written in Kotlin.</p><p>The way I see it: Imagine you’ve got this awesome kitchen cooking machine that effortlessly whips up gourmet meals with just a few button presses, you only need to know which buttons to press and that buttons would be the what and not the how.</p><h3><strong>Talk is cheap. Show me the code. — Linus Torvalds</strong></h3><p>I decided to try to apply that in a side project which is just a sample app in compose using the marvel api at <a href="https://github.com/jeprubio/marvel-compose">https://github.com/jeprubio/marvel-compose</a> .</p><p>Let’s create our first test, HeroListScreenTest, which is a screen with a list of marvel characters. As we are testing composables we can use the libs from the <a href="https://developer.android.com/jetpack/compose/testing">official compose testing documentation</a> and create our composeTestRule in each testing file:</p><pre>class HeroListScreenTest {<br>    @get:Rule<br>    val composeTestRule = createComposeRule()</pre><p>and then pass that compose rule to the robots we use. One test could be:</p><pre>heroListRobot(composeTestRule) {<br>} verify {<br>    characterIsDisplayed(&quot;character 1&quot;)<br>}</pre><p>this way the factory that creates the robot receives the composeTestRule:</p><pre>fun heroListRobot(composeTestRule: ComposeContentTestRule, func: HeroListRobot.() -&gt; Unit) =<br>    HeroListRobot(composeTestRule).apply { func() }</pre><p>and passes it to the robot that can use it to set the layout and perform all the instructions:</p><pre>class HeroListRobot(private val composeTestRule: ComposeContentTestRule) {<br><br>    init {<br>        composeTestRule.setContent {<br>            MarvelComposeTheme {<br>                HeroListScreenContent(...)<br>            }<br>        }<br>    }<br><br>    fun screenAction() {<br>        composeTestRule...<br>    }<br><br>    fun anotherScreenAction() {<br>        composeTestRule...<br>    }<br><br>    infix fun verify(func: HeroListResultRobot.() -&gt; Unit) =<br>        HeroListResultRobot(composeTestRule).apply { func() }<br>}<br><br>class HeroListResultRobot(private val composeTestRule: ComposeContentTestRule) {<br>    fun screenAssertion(name: String) {<br>        composeTestRule....<br>    }<br><br>    fun anotherScreenAssertion() {<br>        composeTestRule...<br>    }<br>}</pre><p>so that we can create our tests using that robots, like the following one:</p><pre>@Test<br>fun whenMagnifierTappedItShowsSearchTextInput() {<br>    heroListRobot(composeTestRule) {<br>        onMagnifierTapped()<br>    } verify {<br>        searchIsDisplayed()<br>    }<br>}</pre><p>In that test we can know the <em>what</em> the test does. It’s using the HeroListRobot (so it’s loading the HeroListScreenContent), it taps on the magnifier and verifies that a search is displayed. How is that done? It doesn’t matter, the robot will know that.</p><p>If we then check the robot code:</p><pre>fun onMagnifierTapped() {<br>    composeTestRule.onNodeWithContentDescription(searchContentDescription).performClick()<br>}</pre><p>we can see that the robot is invoking <em>performClick</em> on that element</p><pre>fun searchIsDisplayed() {<br>    composeTestRule.onNodeWithContentDescription(getString(R.string.search_text)).assertIsDisplayed()<br>}</pre><p>and then verifying the <em>search_text</em> element is displayed.</p><h3>Possible improvement</h3><p>If we don’t want to pass the <em>composeTestRule</em> explicitly to the factory method we can make it an extension function of the test where we want to use it:</p><pre>fun HeroListScreenTest.heroListRobot(func: HeroListRobot.() -&gt; Unit) =<br>    HeroListRobot(composeTestRule).apply { func() }</pre><p>so that then the test reads:</p><pre>@Test<br>fun whenMagnifierTappedItShowsSearchTextInput() {<br>    heroListRobot {<br>        onMagnifierTapped()<br>    } verify {<br>        searchIsDisplayed()<br>    }<br>}</pre><p>However, as robots can be used in more than one test that means they could have multiple factories if we want to do that and use them from multiple tests.</p><p>So I would keep this in mind knowing the pros and cons to decide whether to use this latest approach or not.</p><p>The robot testing pattern facilitates a clearer separation of concerns in testing code. By abstracting implementation details, the tests can concentrate on defining the behaviour of UI components rather than dwelling on the specifics of how actions are executed and assertions are verified. This abstraction enhances test maintainability and readability, contributing to more efficient testing practices for UI tests.</p><p>You can check the <a href="https://github.com/jeprubio/marvel-compose/blob/main/feature-characters/src/androidTest/java/com/rumosoft/characters/presentation/screen/HeroListScreenTest.kt">full screen test</a> used in the sample and <a href="https://github.com/jeprubio/marvel-compose/blob/main/feature-characters/src/androidTest/java/com/rumosoft/characters/presentation/screen/HeroListRobot.kt">the robot</a> that screen uses. And the <a href="https://github.com/jeprubio/marvel-compose">full side project repository</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a49ea9efdfff" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>