Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The correct approach to Android Unit Testing

I know it is a tedious topic for all android developers. But what exactly is the correct approach to Android Testing?

This is what I can picture.

70% Unit testing (JUnit to test all business logic, network layer, database layer etc...)

20% Integration test (Perhaps testing against mock server? mainly testing API results?)

10% UI testing(mock anything else other than the UI interactions, most likely Mockito+Espresso)

Is this what everyone else is following or there is another pattern?

Thanks in advance!

like image 935
WenChao Avatar asked May 13 '15 05:05

WenChao


People also ask

Which is an effective approach of unit level testing?

Unit tests can be performed manually or automated. Those employing a manual method may have an instinctual document made detailing each step in the process; however, automated testing is the more common method to unit tests. Automated approaches commonly use a testing framework to develop test cases.

What should I test in unit tests Android?

Unit tests or small tests only verify a very small portion of the app, such as a method or class. End-to-end tests or big tests verify larger parts of the app at the same time, such as a whole screen or user flow. Medium tests are in between and check the integration between two or more units.

What is the Android testing strategy?

By separating the code into different layers and clearly defining borders with abstractions between them, it is possible to test parts of an Android application independently. It is even possible to completely abstract Android SDK from the application's use cases, so they could be reused for a different platform.

Which framework is more suitable for unit testing Android?

In general, Appium and Calabash are good cross-platform frameworks in testing both your Android and iOS versions at the same time. Espresso is a good choice if you are purely testing Android apps.


1 Answers

This question and my answer, doesn't have anything to do with Android specifically but that is a good thing.

I've slightly modified your assumptions, but the principle is the same.

  • 70% Unit testing (JUnit to test all business logic.)

  • 20% Integration test (network layer, database layer etc, real server)

  • 10% UI testing(UI workflow + manual testing)

Should it be 70%? 80%? 85%? It doesn't matter. The key is the ratio. You want the majority of your tests to be fast, isolated in memory tests. If you do interact with a database you simply want to know your queries work. Does the update query actually update the correct entity? Finally you check your UI works as expected. It doesn't matter at this level what you display. As long as the login screen is displayed when the user isn't logged in your fine.

This is often referred to as the Test Pyramid and is what you have described, just minus the explict ratios.

like image 187
Finglas Avatar answered Sep 20 '22 08:09

Finglas