Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Follow publication

The Future of Writing Tests for Jetpack Compose in Android

Hannah Olukoye
Quick Code
Published in
3 min readJul 1, 2022

--

Credits: https://www.monkeyuser.com/

Are you just getting started with TDD for Android? Test-driven development or TDD is one technique to ensure that tests are included with any new code. When using this method, you write the tests for the addition before you write the implementation code. How much and how to test a component are not governed by any strict rules. In this article, I’ll focus on an instance of how to start adopting TDD when developing an Android app with Jetpack compose.

  1. Illustration — Display a list of items on a screen
  2. What to test/verify
  • The list of items doesn't exist
  • The list items exist
  • Check if an item in the list is a text

3. Writing the tests

Luckily, Compose allows us to test components independently. Previously, to display a huge number of items in lists before Jetpack Compose, you used a RecyclerView and an Adapter. Now, you can utilize LazyColumn or LazyRow to display lists either vertically or horizontally respectively. For this example, you will use LazyColumnto display the list of items. Some of the parameters to test include:

    modifier:
state:
contentPadding:
reverseLayout:
verticalArrangement:
horizontalAlignment:
flingBehavior:
userScrollEnabled:
content:

Step 1 — Select the Compose UI content to be used by calling the setContent method of the ComposeTestRule in the test file.

Step 2 — Pick any two/three parameters from the list above and pass fake data that you can control to see if the tests fail/pass.

Here's what you should have so far.

Step 3 — Run the test on Android Studio to get the status.

Step 4 — You can now proceed to verify the conditions mentioned previously. Remember to name your functions after the particular task you are want to test e.g. lazyColumnTestIfListIsNotEmpty

--

--

Quick Code
Quick Code

Published in Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Hannah Olukoye
Hannah Olukoye

Written by Hannah Olukoye

Google Developer Expert— Android Engineering Manager

No responses yet

Write a response