See Testim Mobile in action. Live demo with Q&A on Apr 29 | Save your spot

Android Testing: A Beginner’s Guide to Getting Started 

Android testing is essential to the dev process for creating reliable, high-quality mobile apps. The testing process calls for verifying…

Testim
By Testim,

Android testing is essential to the dev process for creating reliable, high-quality mobile apps. The testing process calls for verifying functionality works as expected, stability, and a bug-free experience for users. Several types of testing can be performed on Android applications, including unit testing, functional testing, and performance testing. Let’s dive into each type and explore examples of how to implement them.

Android unit testing 

Unit testing is a basic and reliable form of testing that allows devs to verify individual parts of an app by isolating their functionality and confirming assumptions. Testing is performed at the code level and helps ensure each application element functions as expected. Unit tests are typically automated, making them easier to run. 

The JUnit framework is popular with testers, since it comes bundled with the Android SDK. 

Expand Your Test Coverage

Fast and flexible authoring of AI-powered end-to-end tests — built for scale.
Start Testing Free
 

Let’s say you have a simple class called Calculator with a method called add(int a, int b) that returns the sum of two integers. You can create a test class called CalculatorTest that uses JUnit to test this calculation. 

import org.junit.Test;
import static org.junit.Assert.*;

public class CalculatorTest {
    private Calculator calculator = new Calculator();

    @Test
    public void testAdd() {
        assertEquals(5, calculator.add(2, 3));
    }
}

In the example above, we first import the necessary JUnit classes and create an instance of the Calculator class. We then use the @Test annotation to specify that the testAdd() method is a test case. Finally, within the testAdd() method, we use the assertEquals() method provided by JUnit to check if the result of the add(2, 3) call is equal to 5. If it is, the test passes. Otherwise, it fails.

Android functional testing

Functional testing, or UI testing, verifies the entire app works as expected. It’s a black box testing technique for quality assurance that roots its test cases in the software component’s requirements. Since internal program structure is rarely considered while testing functions, input is often provided, with the output then subject to examination. Testing is performed at the application level and helps ensure that it’s stable and follows the recommended user flows.

Functional tests are typically automated as well, which makes them easy to run and helps developers keep their code clean. Espresso is a popular framework for functional testing on Android and it’s also included in the Android SDK.

Espresso tests are run on the device or emulator, interacting with the app the way a user would. You can also automate common testing tasks with the Espresso API.

Let’s jump into an example. Say you have an activity called MainActivity that displays the message “Hello World!” You can create a test class called MainActivityTest that uses Espresso to see if this message displays correctly. 

Building upon the prior example, we can use Espresso’s onView() method to locate the copy, then use the check method to verify the text displayed is indeed “Hello World!”

import androidx.test.Espresso.Espresso;
import androidx.test.Espresso.assertion.ViewAssertions;
import androidx.test.Espresso.matcher.ViewMatchers;
import androidx.test.rule.ActivityTestRule;

import org.junit.Rule;
import org.junit.Test;

public class MainActivityTest {
    @Rule
    public ActivityTestRule<MainActivity> activityRule =
            new ActivityTestRule<>(MainActivity.class);

    @Test
    public void testTextView() {
        Espresso.onView(ViewMatchers.withId(R.id.text_view))
                .check(ViewAssertions.matches(ViewMatchers.withText(“Hello World!”)));
    }
}

To recap, we began with the onView() method to locate the text, followed by using the check method to see if the text displayed is indeed “Hello World!” If it is, the test passes. Otherwise, it fails. 

Android performance testing

Finally, performance testing verifies an app performs well under various conditions, accounting for different device configurations and network conditions. Testing is performed at the application level and helps ensure the application is responsive, free of performance bottlenecks, and is able to handle large amounts of data. 

Several frameworks and tools are available for performance testing and monitoring on Android. If you’re an Android Studio user, you may find the Android Studio Profilers handy for tracking your app’s performance in real time. Additionally, you can use the metric frameworks in the Android SDK to measure your app’s performance.

Let’s say you want to test the performance of your calculateSum() method that takes an array of integers as inputs and returns the sum of these integers. You can use the debug framework to measure the time it takes for the method to finish.

import android.os.Debug;

public class Calculator {
    public int calculateSum(int[] numbers) {
        int sum = 0;
       
        Debug.startMethodTracing(“calculateSum”);
       
        for (int number : numbers) {
            sum += number;
        }
       
        Debug.stopMethodTracing();
       
        return sum;
    }
} 

In the example above, we first import the Android.os.Debug class, which enables method tracing. We then use the startMethodTracing() method to begin tracing the performance of the calculateSum() method. After the calculation is done, we use the stopMethodTracing() method to stop the tracing. The trace file will be generated in the app’s file directory and can be analyzed using the Systrace tool. 

Other testing tools

In addition to JUnit, Espresso, and Android’s performance tools, here’s a rundown of other popular testing tools and frameworks for Android devs. 

Robolectric 

Robolectric is a popular testing framework that allows developers to run unit tests on Android applications without an emulator or a physical device. This can accelerate the testing process by eliminating the need for slower, more resource-intensive process of launching an emulator or connecting to a physical device. Robolectric simulates the Android environment on a Java virtual machine, making it easy to write and run tests for Android SDK features.

UI Automator 

UI Automator is a testing framework that allows developers to create automated UI tests for Android applications. You can call to its API to simulate user input on your app’s interface, testing how your app responds to different conditions and configurations. UI Automator tests are typically slower than unit tests and Espresso tests, but they’re useful if you’re focused on user experience.

Appium 

Appium is an open-source, cross-platform mobile app automation testing framework that can be used for Android and iOS applications. It allows developers to create tests in various programming languages, including Java, Python, Ruby, and C#. It uses the WebDriver protocol, which makes it possible to write tests for both Android and iOS devices. 

Calabash 

Calabash is a test automation framework specifically designed for mobile applications. It allows developers to write tests in natural language and supports Android and iOS platforms. Calabash tests are written in Cucumber, a behavior-driven development (BDD) framework. This makes it easy for even nontechnical stakeholders to understand and maintain tests. 

Monkey 

Monkey is a command line tool included in the Android SDK. You can use it to generate random user input and test the stability of an Android application. Monkey also simulates user interactions such as clicks, touches, and key presses. You can also run it on an emulator or a physical device. This can be useful when testing the application’s ability to handle unexpected input, such as unforeseen taps or swipes. 

These are just a few examples of the tools and frameworks available to Android developers. Which one will work best depends on your project’s requirements and your dev team’s preferences.

Recapping what we covered

Unit testing, functional testing, and performance testing offer test methodologies that serve different purposes, but generally help your team march toward an app that is stable, bug-free, and performant. By utilizing frameworks such as JUnit, Espresso, and Android performance resting, developers can quickly implement and run tests, helping them build better software. Testing is an essential part of the development process for building Android apps your users will love and return to again and again.