Android Testing:

Joaquín Gatti
2 min readJul 7, 2015

The quest for the bugless app

Writing an app has certain implied challenges like building a great UX, making a flexible layout that runs in almost every device, increasing the performance and reduce response time providing an understandable feedback to the user. However the key feature is that the app must run perfectly and bugless.

In the latest Google I/O, the android support testing library was announced. The library provides samples and APIs to run unit and integration tests. This approach is similar to the one we can find in other platforms, but also this path has already been traveled by other plugins like Robolectric. Also when you write unit tests, you’ll need to use mock objects, that imitate real objects running in your device but are obviously used only for test purposes, in this case we’ll need to use Mockito, a library made by Google, that combined with Robolectric makes the perfect testing environment at this point.

Test your app performance and also the code quality.

The worst thing about debugging is debugging itself.

There are different tests that can be run and since we are interested in contributing to our continuous integration process, we will focus on unit tests that will be run without deploying to a physical device or emulator.

Unit tests can be used for different reasons. The most useful cases are testing models, methods or even flows.

@Test
public void onClickAndSwitchTrueOptionA() {
ShadowActivity shadowActivity = shadowOf(mainActivity);

swt.setChecked(Boolean.TRUE);
etName.setText("John Doe");
etEmail.setText("john@doe.com");
etPassword.setText("12345678");
btn.performClick();

Intent startedIntent = shadowActivity.getNextStartedActivity();
ShadowIntent shadowIntent = shadowOf(startedIntent);
Intent expectedIntent = new Intent(mainActivity, OptionAActivity.class);
assertEquals(shadowIntent, expectedIntent);
}

e.g: An activity that must be launched when clicking a button on its ancestor

Bottomline, tests are really useful at the beginning of a project to delineate all the possible scenarios in an app, but also to continuously ensure the correct behavior of an app once many features have been implemented.

Even if it’s common perception that test writing adds adds overhead to a project’s timetable, the time saved in debugging is considerable compared to the time a developer, and also a project manager, will need to fix a bug or re-write an entire flow because of a failure while understanding a functional requirement.

I believe that Google will introduce more tools to encourage developers to release carefully tested apps. Also as the time goes by, we’ll finally migrate to the android support testing library and have all our APIs as part of our android SDK.

All passing tests!

In order to contribute a bit further, I’ve created a github repo that you can download and play with.

Please feel free to contribute!

--

--

Joaquín Gatti

I help companies innovate through design, creativity and disruptive technology.