Android Studio – 38 – Android Testing (JUnit, Espresso)

Testing is a critical aspect of Android app development to ensure the reliability and quality of your applications. Android Studio provides robust testing frameworks like JUnit and Espresso that help you perform various types of testing, including unit testing, integration testing, and UI testing. These testing tools enable you to identify and address issues in your app’s functionality and user interface, ensuring a seamless user experience. In this guide, we will explore Android testing in Android Studio, focusing on JUnit for unit testing and Espresso for UI testing. We’ll cover the significance of testing, setting up tests, and executing them, all accompanied by code examples and commands for illustration.

Significance of Testing in Android Development

Testing is crucial for several reasons:

  1. Bug Detection: Testing helps identify and fix bugs and issues in your code, preventing crashes and unexpected behavior.
  2. Regression Testing: Ensures that new code changes do not introduce new issues or break existing functionality.
  3. Quality Assurance: Validates that your app meets the desired quality and functionality standards.
  4. User Experience: Testing ensures that your app provides a smooth and user-friendly experience.

Unit Testing with JUnit

JUnit is a widely used testing framework for writing and running unit tests in Android apps. Unit tests focus on testing individual units of code, such as methods or functions, in isolation. To perform unit testing with JUnit in Android Studio:

1. Create a Test Class:

Create a test class in the src/test/java directory with the same package structure as the class you want to test. For example, if you want to test a class in the com.example.myapp package, create a test class in src/test/java/com/example/myapp.

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

public class MyUnitTest {
    @Test
    public void addition_isCorrect() {
        assertEquals(4, 2 + 2);
    }
}

In this example, a simple unit test is created to verify that addition works correctly.

2. Run the Tests:

To execute the unit tests, right-click the test class file and select “Run” or use the ./gradlew test command in the terminal. Android Studio will execute the tests and display the results in the “Run” window.

UI Testing with Espresso

Espresso is a testing framework for creating and running UI tests in Android apps. UI tests focus on validating the user interface and user interactions. To perform UI testing with Espresso in Android Studio:

1. Create a UI Test Class:

Create a UI test class in the src/androidTest/java directory. Espresso tests typically target a specific activity or fragment of your app.

import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4ClassRunner.class)
public class MyUITest {
    @Rule
    public ActivityScenarioRule<MainActivity> activityRule
            = new ActivityScenarioRule<>(MainActivity.class);

    @Test
    public void clickButton_opensNewActivity() {
        onView(withId(R.id.myButton)).perform(click());
        onView(withId(R.id.newActivityLayout)).check(matches(isDisplayed()));
    }
}

In this example, a UI test is created to verify that clicking a button in the MainActivity opens a new activity.

2. Run the UI Tests:

To execute the UI tests, right-click the test class file and select “Run” or use the ./gradlew connectedAndroidTest command in the terminal. Android Studio will launch an emulator or device to run the tests and display the results in the “Run” window.

Test Report and Coverage

Android Studio provides a test report that summarizes the results of your tests, including any failures or errors. You can access this report by opening the “Run” window and clicking on the “Tests” tab.

Additionally, you can measure test coverage to assess the extent of your code that is covered by tests. To enable code coverage, add the following line to your app’s build.gradle file:

debug { testCoverageEnabled true }

After running your tests, you can view code coverage reports in the “Run” window.

Continuous Integration (CI)

Integrating testing into your continuous integration (CI) pipeline is essential for ensuring that your app remains stable and reliable during development. Popular CI platforms like Jenkins, Travis CI, and CircleCI can be configured to automatically run your tests whenever changes are pushed to your source code repository.

Conclusion

Testing is a fundamental aspect of Android app development, and Android Studio provides powerful testing frameworks like JUnit and Espresso to help you ensure the quality and reliability of your applications. By implementing unit tests to verify individual code units and UI tests to validate the user interface and user interactions, you can identify and address issues early in the development process, resulting in a more robust and user-friendly app. Regularly running tests, generating reports, and integrating testing into your CI pipeline are essential practices for maintaining the integrity of your Android apps.