Fundamentals 101: Testing in Android

Abhijith Sreekar
4 min readJan 3, 2020

--

What is Testing?

A process in software development that allows us to check the correctness of the app features.

Testing your app is an integral part of the app development process. By running tests against your app consistently, you can verify your app’s correctness, functional behavior, and usability before you release it publicly.

Testing also provides you with the following advantages:

  • Rapid feedback on failures.
  • Early failure detection in the development cycle.
  • Safer code refactoring, letting you optimize code without worrying about regressions.
  • Stable development velocity, helping you minimize technical debt.
  • Force you to follow Architecture.

Types of tests in Android:

  • Local Unit tests
    - The smallest possible testable piece of code part of a specific independent module
    - Runs on local JVM (don’t require a device/emulator)
  • Instrumentation Tests
    - Testing more than one individual module working together that implements an entirely functional part of our system
    - Runs on a device/emulator
    * UI testing — actual testing we do it on the android devices
    * Integrated testing — testing our units with Android components units
    — e.g., DB testing, content providers testing, services testing, shared pref testing

Testing Pyramid:

The below pyramid gives you a better idea of the different types of tests you need to write and how they differ from each other. They are also categorized as small tests (local unit tests, unto 100ms), medium tests (integrated tests, 100ms — 2s), and large tests (UI tests/end to end tests, more than 2s).

As we go up the pyramid, the scenario becomes more real, as UI testing will be similar to any user action. This is also known as fidelity.

Fidelity — Extent to which a test duplicate the actual conditions or tasks performed. The closer the match, the higher the fidelity of the test.

Testing Pyramid

Because unit tests run on local JVM, they run fast, and so, their fidelity is lower, meaning they act less like they would in the real world. Whereas Instrumentation tests run on real or emulated Android devices, they reflect what will happen in the real world, because of which they have a high fidelity rate and are much slower.

Folder Structure:

Upon opening an Android project, you can find these three folders in the project pane:

* com.example.android.testingapp (main)- Contains your app code
* com.example.android.testingapp (androidTest)- Contains instrumented tests.
* com.example.android.testingapp (test)- Contains local tests

These folders are known as Source sets. Source sets are folders containing source code for your app. The source sets, which are colored green (androidTest and test), contain your tests.

Different libraries used for testing in Android:

  • JUnit — An open-source Unit Testing Framework for JAVA. Contain functions like assert etc
  • Mockito — Mockito is a popular mock framework that can be used in conjunction with JUnit. Mockito allows you to create and configure mock objects.
  • PowerMockito — It is a PowerMock’s extension API to support Mockito. It provides capabilities to work with the Java Reflection API in a simple way to overcome the problems of Mockito, such as the lack of ability to mock final, static, or private methods.
  • Espresso — A framework which provides APIs for writing UI tests to simulate user interactions within a single target app in isolation
  • UI Automator — A framework that provides a set of APIs to build UI tests that perform interactions on user apps and system apps. The UI Automator APIs allow you to perform operations such as opening the Settings menu or the app launcher in a test device.
  • Roboelectric — Any unit tests (typically using JUnit) that contain Android components (like context inside a validator class, etc.) and need to be tested, we use roboelectric. It installs the Android SDK in JVM and runs the tests.
  • Hamcrest (Matcher) — Hamcrest allows checking for conditions in your code via existing matchers classes. It also supports creating customized assertion matchers allowing match rules to be defined declaratively.

What is Code Coverage?

Code coverage is a measure that describes the degree of which the source code of the program has been tested.

Android Studio has a built-in feature that allows you to run tests with code coverage. Navigate to the `src/test/java` folder and right-click. Then select Run ‘Tests in ‘java’’ with Coverage.

Also, there are libraries like Jacoco ( Java Code Coverage), one of the most used tools in Java for this purpose.

What next?

I just covered the tip of the iceberg in this article. Also, there are many other great testing frameworks for you to explore that haven’t been explained in this article. I will try to add-in more articles about testing, which includes code samples in the future.

Conclusion:

I hope this article helped you to understand the basics of testing in Android and encourages you to write and to conduct tests for your app.
Happy Coding!

--

--

Abhijith Sreekar

Another mechanical engineer turned software engineer. Senior Developer @ XAM Consulting