android-testing Getting started with android-testing


This section provides an overview of what android-testing is, and why a developer might want to use it.

It should also mention any large subjects within android-testing, and link out to the related topics. Since the Documentation for android-testing is new, you may need to create initial versions of those related topics.

Test Types

Android tests are based on JUnit, and you can run them either as local unit tests on the JVM or as instrumented tests on an Android device. This page provides an introduction to the concepts and tools for building Android tests

  • Local unit tests (Located at module-name/src/test/java/)
  • Instrumented tests (Located at module-name/src/androidTest/java/)

Local unit tests JUnit

public class MainActivityInstrumentationTest {

    public ActivityTestRule mActivityRule = new ActivityTestRule<>(

    public void sayHello(){
        onView(withText("Say hello!")).perform(click());

        onView(withId("Hello, World!")));

Instrumented tests Example dependencies { androidTestCompile '' androidTestCompile '' androidTestCompile '' // Optional -- Hamcrest library androidTestCompile 'org.hamcrest:hamcrest-library:1.3' // Optional -- UI testing with Espresso androidTestCompile '' // Optional -- UI testing with UI Automator androidTestCompile '' }

android { defaultConfig { testInstrumentationRunner "" } }

Instrumented Unit Test Class

import android.os.Parcel;
import android.util.Pair;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import static;
import static org.junit.Assert.assertThat;

public class LogHistoryAndroidUnitTest {

    public static final String TEST_STRING = "This is a string";
    public static final long TEST_LONG = 12345678L;
    private LogHistory mLogHistory;

    public void createLogHistory() {
        mLogHistory = new LogHistory();

    public void logHistory_ParcelableWriteRead() {
        // Set up the Parcelable object to send and receive.
        mLogHistory.addEntry(TEST_STRING, TEST_LONG);

        // Write the data.
        Parcel parcel = Parcel.obtain();
        mLogHistory.writeToParcel(parcel, mLogHistory.describeContents());

        // After you're done with writing, you need to reset the parcel for reading.

        // Read the data.
        LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel);
        List<Pair<String, Long>> createdFromParcelData = createdFromParcel.getData();

        // Verify that the received data is correct.
        assertThat(createdFromParcelData.size(), is(1));
        assertThat(createdFromParcelData.get(0).first, is(TEST_STRING));
        assertThat(createdFromParcelData.get(0).second, is(TEST_LONG));