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. About Me

  3. How to Test Writing the tests What to Test Questions??

    1 3 2 4 Today's Agenda
  4. Test Driven Development (TDD) Red Refactor Green

  5. Example: Display a list of items

  6. ★ The list is empty ★ The list is not

    empty ★ Verify if an item in the list is a text What to test?
  7. RecyclerView vs LazyColumn Jetpack Compose

  8. ★ LazyColumn Parameters Writing tests modifier: state: contentPadding: reverseLayout: verticalArrangement:

    horizontalAlignment: flingBehavior: userScrollEnabled: content:
  9. 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") } } } } }
  10. Run the test on Android Studio

  11. Failed test

  12. Passed test

  13. 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") } } } } }
  14. 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() } } }
  15. 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) } } } }
  16. Preview

  17. Resources • https://developer.android.com/codelabs/jetpack-compose-basics • https://www.wwt.com/article/test-driven-development-with-android • https://blog.canopas.com/how-to-use-tdd-effectively-to-write-android-integration-tests-5e fc7ee48ba8 • https://paulallies.medium.com/getting-started-with-tdd-kotlin-and-jetpack-compose-887c

    04d733f9
  18. Thank you! Twitter: @hannah_omu