Member-only story
Android Unit Testing Basics
Learn how to start writing unit tests in Android

In mobile application development, we have many trending things which are in progress like MVVM, Jetpack, KMM(Kotlin Multiplatform), etc. Testing is one of the important and preferred things nowadays. Testing has become a crucial factor in development. There are multiple strategies for app testing. In most of the interviews, the knowledge of writing unit test cases has become a must. So in this post let’s see what is unit testing and some basics of writing local unit test cases.
As everything can’t be covered in a single post I will be writing a series on testing. If you want to jump to code please check out the Github repo: android_unit_tests
Do we need to write Unit Test Cases?
The answer would be yes to most of the cases. As we have faster development cycles running to meet product requirements we are out of this scope in most cases or not adopting it as we haven’t started. However, out there many companies are following the approach of writing unit tests for achieving better code quality in order to build great products.
Even writing unit tests helps to resolve bugs and on the other hand, writing test cases can also help to resolve issues related to existing code changes of which we might not be aware.
What is Unit Testing?
Unit testing means testing the written code by small parts. We usually write programs that have classes and further the logic in classes is divided into methods. Unit refers to a small piece of code that can be either method, class, or component. The aim of unit tests is to be to verify the logic of individual units.
How to write Simple Unit Test
We use the JUnit
framework to write simple unit tests. Let’s see how to write a simple unit test for a method that validates the given email. For this first, we need to know where to write these tests.
We usually have a project structure in Android Studio where we use
- The
main
folder for writing the application logic. - The
test
folder is used for writing unit tests.