Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Future of Writing Tests for Jetpack Compose in Android

The Future of Writing Tests for Jetpack Compose in Android

There are no hard and fast rules about how much and how to test a component. In my talk, I'll show you how to start using Test Driven Development for an Android app using Jetpack compose.
When using the TDD approach, one begins writing the addition tests before the implementation code.

Hannah Olukoye

November 25, 2022
Tweet

More Decks by Hannah Olukoye

Other Decks in Technology

Transcript

  1. Android Speaker Image Placeholder The Future of Writing Tests for

    Jetpack Compose in Android Hannah Olukoye
  2. ★ The list is empty ★ The list is not

    empty ★ Verify if an item in the list is a text What to test?
  3. val tagDescTest = "LazyColumnItemTest" @RunWith(AndroidJUnit4::class) class LazyColumnTest { @get:Rule val

    composeTestRule = createComposeRule() @Test fun lazyColumnTestBackgroundColour() { composeTestRule.setContent { Surface(color = MaterialTheme.colors.background) { LazyColumn( modifier = Modifier.testTag(tagDescTest), flingBehavior = ScrollableDefaults.flingBehavior() ) { items(1) { index -> Text(text = "Item: $index") } } } } }
  4. val tagDescTest = "LazyColumnItemTest" @RunWith(AndroidJUnit4::class) class LazyColumnTest { @get:Rule val

    composeTestRule = createComposeRule() @Test fun lazyColumnTestBackgroundColour() { composeTestRule.setContent { Surface(color = MaterialTheme.colors.background) { LazyColumn( modifier = Modifier.testTag(tagDescTest), flingBehavior = ScrollableDefaults.flingBehavior() ) { items(1) { index -> Text(text = "Item: $index") } } } } }
  5. Implementation - Definition data class Demo(val description: String) val demo

    = listOf( Demo("Item 1"), Demo("Item 2"), Demo("Item 3"), Demo("Item 4") ) class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { DisplayList(demo = demo) DummyApp() } } }
  6. Implementation - Integration @Preview @Composable fun DummyApp() { DisplayList(demo =

    demo) } @Composable fun DisplayList(demo: List<Demo>) { Surface(color = MaterialTheme.colors.background) { LazyColumn(modifier = Modifier.testTag(description_tag), flingBehavior = ScrollableDefaults .flingBehavior() ) { items(demo) { demo -> Text(text = demo.description) } } } }