In order to use AndroidX Test, you need to include several testing specific libraries. This guide explains how to declare these libraries in your app's project files and shows how AndroidX Test libraries are organized.
Add AndroidX Test libraries
In order to use AndroidX Test, you must modify your app project's classpath dependencies and manifest within your development environment, as shown in the following sections.
Add Gradle dependencies
To modify your app project's classpath dependencies, complete the following steps:
- Open the
build.gradle
file for your app. -
In the
repositories
section, make sure Google's Maven repository appears:allprojects { repositories { jcenter() google() } }
-
For each AndroidX Test package you want to use, add its package name to the
dependencies
section. For example, to add theespresso-core
package, add the following lines:dependencies { ... androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' }
Caution: Using dynamic dependencies (for example,
espresso-core:3.1.0+
) can cause unexpected version updates and regression incompatibilities. We recommend that you explicitly specify a particular version number (for example,espresso-core:3.1.0
). -
If your app builds tests that rely on JUnit-based classes, such as
Assert
andTestSuiteLoader
, add the following lines in theandroid
section of the file:android { ... // Gradle automatically adds 'android.test.runner' as a dependency. useLibrary 'android.test.runner' useLibrary 'android.test.base' useLibrary 'android.test.mock' }
Note: You only need to include the libraries that contain the classes used in your app. For a list of the classes that appear in each library, see JUnit-based libraries.
Add manifest declarations
To run tests that rely on JUnit-based 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 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 (API level 28) or higher.
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" />
List of AndroidX Test dependencies
AndroidX Test includes many dependencies, as shown in the following sections.
Gradle dependencies
The following Gradle-based dependencies are available in AndroidX Test:
dependencies {
// Core library
androidTestImplementation 'androidx.test:core:1.0.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.0'
// 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:3.1.0'
}
As discussed in Adding Gradle
dependencies from a Maven repository, you can add
these dependencies to your development environment by adding each dependency
within your module-level build.gradle
file.
JUnit-based libraries
The lists in this section show how the platform organizes JUnit-based classes in Android 9 (API level 28) and higher.
android.test.base
This library consists of the following classes, each of which is in the
junit.framework
package:
Assert
AssertionFailedError
ComparisonFailure
Protectable
Test
TestCase
TestFailure
TestListener
TestResult
TestSuite
The library also consists of the following classes that are deprecated as of Android 9 (API level 28):
android.test
package
AndroidTestCase
FlakyTest
InstrumentationTestCase
InstrumentationTestSuite
PerformanceTestCase
UiThreadTest
android.test.suitebuilder.annotation
package
com.android.internal.util
package
android.test.runner
This library consists of the following classes:
android.test
package
junit.runner
package
The library also consists of the following classes that are deprecated as of Android 9 (API level 28):
android.test
package
ActivityInstrumentationTestCase
ActivityInstrumentationTestCase2
ActivityTestCase
ActivityUnitTestCase
AndroidTestRunner
ApplicationTestCase
AssertionFailedError
ComparisonFailure
InstrumentationTestRunner
IsolatedContext
MoreAsserts
ProviderTestCase
RenamingDelegatingContext
ServiceTestCase
SingleLaunchActivityTestCase
SyncBaseInstrumentation
TestSuiteProvider
TouchUtils
ViewAsserts
android.test.suitebuilder
package
android.test.mock
This library consists of the following classes, each of which is in the
android.test.mock
package:
The library also consists of the following classes that are deprecated as of
Android 9 (API level 28). Each class is located in the
android.test.mock
package: