0

I recently migrated my Android pet project from View to Compose UI and at the begining I was excited about all the @Compose and @Preview functions but righ away noticed a segnificant drop in the repo's Code Coverage.

I wrote some tests to compensate for the loss, but later discovered that those tests were not calculated as coverage—probably because of all the lambdas in Compose functions!

In search for any potential answers, I just found this thread which didn't help that much:

https://github.com/jacoco/jacoco/issues/1208

Here is my Android app repo link:

https://github.com/mohsenoid/Rick-and-Morty

and here is the Code Coverage report:

https://app.codecov.io/gh/mohsenoid/Rick-and-Morty

if you have a potential solution or would like to check:

enter image description here

===== An example for the question improvements =====

I have this Composable UI function:

@VisibleForTesting
@Composable
fun EpisodeItem(
    episode: Episode,
    onEpisodeClicked: (Episode) -> Unit = {},
) {
    Card(
        modifier =
            Modifier
                .fillMaxWidth()
                .clickable { onEpisodeClicked(episode) },
    ) {
        Row(modifier = Modifier.height(80.dp)) {
            Box(
                modifier =
                    Modifier
                        .fillMaxHeight()
                        .width(100.dp)
                        .background(MaterialTheme.colorScheme.inverseOnSurface),
                contentAlignment = Alignment.Center,
            ) {
                Text(
                    text = episode.episode,
                    style = MaterialTheme.typography.titleLarge,
                )
            }

            Column(
                modifier =
                    Modifier
                        .fillMaxHeight()
                        .weight(1f)
                        .padding(8.dp),
            ) {
                Text(
                    text = episode.airDate,
                    modifier = Modifier.fillMaxWidth(),
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    style = MaterialTheme.typography.labelMedium,
                )
                Spacer(modifier = Modifier.height(4.dp))
                Text(
                    text = episode.name,
                    modifier =
                        Modifier
                            .fillMaxWidth(),
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis,
                    style = MaterialTheme.typography.titleMedium,
                )
            }
        }
    }
}

and this test for it:

@RunWith(RobolectricTestRunner::class)
class EpisodeScreenTest {
    @get:Rule
    val rule = createComposeRule()

    @Test
    fun `When content is EpisodeItem, Then it should display EpisodeItem name, airDate and episode`() {
        // GIVEN

        // WHEN
        rule.setContent {
            EpisodeItem(
                episode =
                    createEpisode(
                        name = TEST_EPISODE_NAME,
                        airDate = TEST_EPISODE_AIR_DATE,
                        episode = TEST_EPISODE_EPISODE,
                    ),
            )
        }

        // THEN
        rule.onNodeWithText(TEST_EPISODE_NAME).assertExists()
        rule.onNodeWithText(TEST_EPISODE_AIR_DATE).assertExists()
        rule.onNodeWithText(TEST_EPISODE_EPISODE).assertExists()
    }

    companion object {
        private const val TEST_EPISODE_NAME = "Pilot"
        private const val TEST_EPISODE_AIR_DATE = "December 2, 2013"
        private const val TEST_EPISODE_EPISODE = "S01E01"
    }
}

The test passes but doesn't affect the code coverage.

FYI, I confirmed that it is not the Robolectric effect.

3
  • 2
    Please provide a minimal reproducible example of an instrumentation test with your setup to measure code coverage where you would expect a block of code to be registered as covered where it actually isn't.
    – Leviathan
    Commented Jun 24 at 23:46
  • 1
    Do you even need to include testing Composables to your code coverage? Also, based on the test code you posted, you might be better off doing screenshot testing, see Paparazzi: github.com/cashapp/paparazzi Commented Jul 11 at 6:26
  • Thanks @AlvinDizon, I did screenshot testing using the new compose UI screenshot testing, but in the end, it won't raise the coverage number. The good news is that based on the Jacoco devs talk at Droidcon Berlin 2024 the upcoming Jacoco version is going to fix that soon. Commented 2 days ago

0