Skip to content

Learn

Android testing: A beginner's guide to getting started

Unsure which Android testing layer catches which bug? Unit, UI & performance tests explained with runnable Java code for JUnit, Espresso & method tracing.

android testing

Android testing is essential to the development process for creating high-quality, reliable mobile applications. The testing process involves verifying that a function works as expected and that it’s stable and free of bugs. Several types of testing can be performed on Android applications, including unit testing, functional testing, and performance testing. In this article, we’ll go through each type and provide code examples for how to implement them.

Android Unit Testing

Unit testing is considered one of the most basic types of testing, and it can also be the most reliable. It allows developers to verify the individual components of an application by isolating their functionality and giving them the tools to confirm their assumptions. Testing is performed at the code level and helps ensure that each application element functions correctly. In addition, unit tests are typically automated, so they’re easy to run.

The JUnit framework is a popular choice for unit testing on Android, and it comes bundled with the Android SDK.

Example

To illustrate, 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 the add method.

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, is the next level up, and it’s used to verify that the entire application works as expected. It’s a black box testing technique for quality assurance that bases its test cases on the software component’s requirements. Since internal program structure is rarely considered while testing functions, input is often provided, and the output is then examined. Testing is performed at the application level and helps ensure that it’s stable, has no bugs, and follows the designed user flows.

Functional tests are also typically automated, which makes them easy to run and helps developers keep their code robust and 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. They interact with the application the way a human user does. In addition, Espresso provides APIs to find and interact with the views in the app.

Example of Android Testing

Let’s say you have an activity called MainActivity that displays a text view with the message “Hello World!” You can create a test class called MainActivityTest that uses Espresso to see if the text view displays the correct message.

Continuing from the previous example, we can use Espresso’s onView() method to find the text view in the layout and then use the check method to check if 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!")));
    }
}

In the example above, we first use the onView() method to find the text view in the layout by its ID. We then use the check method to see if the text displayed is indeed “Hello World!” If it is, the test passes. Otherwise, it fails.

Android Performance

Finally, performance testing verifies that an application performs well under different conditions, such as various device configurations and network conditions. Testing is performed at the application level and helps ensure that the application is responsive, has no performance bottlenecks, and can 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 when keeping tabs on your app’s performance in real time and want to see a trace of all its changes. Additionally, you can use the included metric frameworks in the Android SDK to measure your app’s performance.

Example

Let’s say you want to test the performance of your calculateSum() method that takes an array of integers as input and returns the sum of all the 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 provides 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, there are several other popular testing tools and frameworks for Android development. Some of the most notable include the following:

Robolectric

Robolectric is a popular testing framework that allows developers to run unit tests on Android applications without needing an emulator or a physical device. This can significantly speed up the testing process as it eliminates the need for the 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. It provides APIs that can interact with the application’s user interface by simulating user input, and you can use it to test the application’s behavior under different conditions and configurations. UI Automator tests are typically slower than unit tests and Espresso tests, but they can help test the 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 to interact with the application, which makes it possible to write tests that can be run on 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 can also simulate 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 many testing tools and frameworks available for Android development. Which one will work best depends on your project’s specific requirements and your development team’s preferences.

In Conclusion

Testing is an essential part of the development process for creating high-quality, reliable Android applications. You can perform several types of testing on Android applications, including unit testing, functional testing, and performance testing. Each type serves a different purpose and helps ensure that the application is stable, free of bugs, and performs well. By utilizing frameworks such as JUnit, Espresso, and Android performance resting, developers can quickly implement and run tests, ultimately helping them create better quality software.

Author:

Guest Contributors

Date: Sep. 23, 2026

You may also be interested in...