Set up project for AndroidX Test

AndroidX Test is a collection of Jetpack libraries that lets you run tests against Android apps. It also provides a series of tools to help you write these tests.

For example, AndroidX Test provides JUnit4 rules to start activities and interact with them in JUnit4 tests. It also contains UI Testing frameworks such as Espresso, UI Automator and the Robolectric simulator.

Add AndroidX Test libraries

In order to use AndroidX Test, you must modify your app project's dependencies within your development environment.

Add Gradle dependencies

To modify your app project's dependencies, complete the following steps:

  • Step 1: Open the build.gradle file for your Gradle module.
  • Step 2: In the repositories section, make sure Google's Maven repository appears:
  allprojects {
    repositories {
      jcenter()
      google()
    }
  }
  • Step 3: For each AndroidX Test package you want to use, add its package name to the dependencies section. For example, to add the espresso-core package, add the following lines:

Groovy

dependencies {
        ...
        androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
    }

Kotlin

dependencies {
        ...
        androidTestImplementation('androidx.test.espresso:espresso-core:$espressoVersion')
    }

These are the most common AndroidX Test dependencies available:

Groovy

dependencies {
    // Core library
    androidTestImplementation "androidx.test:core:$androidXTestVersion0"

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation "androidx.test:runner:$testRunnerVersion"
    androidTestImplementation "androidx.test:rules:$testRulesVersion"

    // Assertions
    androidTestImplementation "androidx.test.ext:junit:$testJunitVersion"
    androidTestImplementation "androidx.test.ext:truth:$truthVersion"

    // Espresso dependencies
    androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-web:$espressoVersion"
    androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$espressoVersion"

    // The following Espresso dependency can be either "implementation",
    // or "androidTestImplementation", depending on whether you want the
    // dependency to appear on your APK’"s compile classpath or the test APK
    // classpath.
    androidTestImplementation "androidx.test.espresso:espresso-idling-resource:$espressoVersion"
}

Kotlin

dependencies {
    // Core library
    androidTestImplementation("androidx.test:core:$androidXTestVersion")

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation("androidx.test:runner:$testRunnerVersion")
    androidTestImplementation("androidx.test:rules:$testRulesVersion")

    // Assertions
    androidTestImplementation("androidx.test.ext:junit:$testJunitVersion")
    androidTestImplementation("androidx.test.ext:truth:$truthVersion")

    // Espresso dependencies
    androidTestImplementation( "androidx.test.espresso:espresso-core:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-contrib:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-intents:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-accessibility:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-web:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso.idling:idling-concurrent:$espressoVersion")

    // The following Espresso dependency can be either "implementation",
    // or "androidTestImplementation", depending on whether you want the
    // dependency to appear on your APK"s compile classpath or the test APK
    // classpath.
    androidTestImplementation( "androidx.test.espresso:espresso-idling-resource:$espressoVersion")
}

The Release Notes page contains a table with the latest versions per artifact.

Refer to the Package Index or Class Index for specific reference documentation on these libraries.

Projects using deprecated classes

If your app uses tests that rely on deprecated JUnit3-based android.test classes , such as InstrumentationTestCase and TestSuiteLoader, add the following lines in the android section of the file:

android {
    ...
    useLibrary 'android.test.runner'

    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
  }

Add manifest declarations

To run tests that rely on deprecated JUnit3-based android.test classes, add the necessary <uses-library> elements to your test app's manifest. For example, if you add tests that depend on the android.test.runner library, add the following element to your app's manifest:

<!-- You don't need to include android:required="false" if your app's

   minSdkVersion is 28 or higher. -->

<uses-library android:name="android.test.runner"

       android:required="false" />

To determine the library that contains a given JUnit-based class, see JUnit-based libraries.

Considerations when using deprecated classes and targeting Android 9 or

higher

The guidance in this section applies only if you target Android 9 (API level 28) or higher and the minimum SDK version for your app is set to Android 9.

The android.test.runner library implicitly depends on the android.test.base and android.test.mock libraries. If your app only uses classes from android.test.base or android.test.mock, you can include the libraries by themselves:

<!-- For both of these declarations, you don't need to include
   android:required="false" if your app's minSdkVersion is 28
   or higher. -->

<uses-library android:name="android.test.base"
       android:required="false" />
<uses-library android:name="android.test.mock"
       android:required="false" />