If you want to test a source code of an Android application, you should consider testing it in three different categories. Every category of Android testing levels is required to be able to say with confidence there is only a small risk that something does not work correctly inside the app. Just check the test pyramid below to get a clear picture of what I am talking about,
You should always test the bottom level of the source code first, which can be done by using classic and simple unit tests with which you test the methods in java classes. To be more formal, in the Android testing process that kind of tests are called small tests. The second level is integration tests with which you test interface and interaction between two (or more) different components; that kind of tests are called medium tests. The last level is large tests and they test use cases, API calls and similar more complex features that you have in your app.
So let’s start with a unit test. What do you need to execute it? Well, nothing special. That kind of test you can execute on your computer, you don’t need to execute it on an Android device or some emulator. It tests just pure java code, and that is done on a computer because it does not need to use Android SDK or any kind of Android operations. Because of that, unit tests are fast and you can use them whenever you want since it’s a fast and straightforward process. For example, integration tests use Android SDK so you don’t have a choice, you have to execute them on an Android device or emulator. That means they are a bit slower than unit tests since you have to wait for the app to build on a device (or emulator).
However, it does not matter whether tests execute fast or slow, we need them all. If we write only unit tests because they execute faster, we can’t have confidence that the source code is written well. Every level of tests has different characteristics which makes them special and it’s never a good decision to skip some of them. I mean, it’s not only a problem if you have unit tests but miss integration tests, it’s also a problem vice versa (having integration tests but without unit tests). In that example, you can be sure that your components communicate together as they should, but there is no way to be sure that simple methods in every component work correctly. Long story short, you need to write all three levels of tests, it should always be a combination of small, medium and large tests. In official Android documentation we found a recommendation on the percentage of tests every level should have:
70% small tests
20% medium tests
10% large tests
Small level tests
The lowest level of tests are unit tests, they execute on a computer so you don’t need a device or emulator and they are used for testing pure java methods and source code. With this kind of tests you can be sure that your methods do exactly what they need to do. The best advantage of these tests is the fact they execute really fast because there is no need for Android OS or SDK. However, there is a problem of not being sure whether your app is doing what it should on a higher level, so you can’t have only this kind of tests and say you have tested the app.
When you write unit tests you should be aware that they should be used only for pure Java source code and no Android SDK interactions should be included. Of course, we all know that it is really complicated to write source code for Android app without interaction with SDK, because of that you should use Mockito that will enable you to call methods from Android SDK but without real implementation. Otherwise, if you want and need to use an implementation of Android SDK methods, then you should use Robolectric.
Medium level tests
After you wrote small level tests, it’s time for the upper level. So, medium level tests can tell you if your app components interact with each other in the right way, or if they don’t. The answer to that question you should find out that with this level of tests.
The bad thing about this kind of tests is that they execute slower than unit tests. The reason behind that is the fact they use Android SDK while executing, but, on the bright side we can use Android SDK while writing it. So, the most popular use cases for testing are some API tests, database, forms etc…
Large level tests
The highest level of tests on Android is large test level with which you test use cases. Large tests are slow, and they need an Android device or emulator for execution since only with them we can test the app in a way the user sees it. Here you should test UI components, and to do that, you can use Espresso library that will help you to make a whole process much easier and smoother.
My two cents
Most Android dev teams use only one level of tests, most time only small level. Is that wrong? Well, you know, it’s better than nothing but there is not enough to be able to say with confidence that your app has tests. That kind of tests can help, but in the name of software quality, is nothing. Our recommendation for Android testing levels use is always to use all levels in the needed percentage. That way, the quality is the first priority.