A Beginner's guide to automated Testing in Android(with compose)


As a junior android developer learning Kotlin and Jetpack Compose, one area I’ve recently explored is automated testing. It may sound complex at first, but with the right understanding, it becomes a powerful tool in building robust and maintainable apps.
What are automated tests?
These are codes that verify that your app behaves as expected i.e, automatically. Instead of manually testing features by clicking around the app, tests run predefined checks, saving time and reducing human error.
Why are automated tests important?
1) They enable one to catch bugs early— during development, not after release.
2) Speed up development— saves on time by reducing the need for repeating same manual tests.
3) Ensure stability— especially when refactoring or adding new features.
4) Make onboarding easier—new team members can rely on existing tests.
Types of Tests:
We have two main types of tests in android dev which are:
1) Local Tests
2) Instrumentation tests
The table below shows the differences between the two.
Type | Local Test | Instrumentation Test |
Runs on | Local JVM (no emulator/device) | Emulator or physical Android device |
Speed | Fast | Slower |
Android APIs allowed | No | Yes |
UI Testing | Not possible | Fully supported |
Best practices for Writing automated tests:
1) Name tests clearly— Describe what the test does.
2) Keep them small and focused— One test, one behaviour.
3) Avoid hardcoding values — Use constants or readable formats.
4) Test both expected and unexpected input— Cover edge cases.
5) Use assertions to verify results.
Where to place tests in your android project:
Local Tests go in:
src/test/java/com/example/yourapp/
Instrumentation tests go in:
src/androidTest/java/com/example/yourapp/
Local Tests = logic-only(no Android stuff)
Instrumented Tests = anything involving UI or Android SDK.
How to create a Test Method:
To create a test method, define a function inside a test class and annotate it with @Test
. This method should perform operations and use assertions to verify the behavior of your code. Below is an example of a code snippet.
Creating Test Classes:
Local Test class:
For local tests, create a class in the test directory.
For UI or Android tests, create one in androidTest.
Both should include test methods that check the behavior of app components.
Instrumentation Test classes:
The instrumentation tests are basically created in the same manner with local tests.
Making Assertions:
Assertions are used to check whether the actual result matches what you expected. They help verify logic in local tests or confirm UI elements are displayed correctly in instrumentation tests.
Local test:
Instrumentation test:
Using Test Rules:
Test rules manage test behavior. For Jetpack Compose, a common rule is ComposeTestRule, which launches and handles composables in a test environment.
The above code sets up your Compose test environment automatically.
Using ComposeTestRule to launch composables:
In UI tests, you use ComposeTestRule to display a composable on screen so you can interact with and test it. You pass your composable to it in a setup function using setContent.
Interacting with composables in UI tests:
Compose testing allows you to simulate user input, like typing in a TextField or clicking a button. You identify composables using their text labels or test tags and perform actions like performTextInput or click.
Bonus Tip, Use useUnmergedTree = true if the test can’t find deeply nested elements.
How to run the tests:
From Android studio, right-click the test class or method then click on “RunTest”.
Final Thoughts
Learning to test your android app early in your journey saves you time, helps you write cleaner code, and prepares you for professional development environments. Just start small, even with testing one function or screen can be valuable.
Subscribe to my newsletter
Read articles from David Njoka directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

David Njoka
David Njoka
Passionate about Jetpack Compose, coroutines, and making tech fun.