AndroidX Test è una raccolta di librerie Jetpack che ti consente di eseguire test sulle app per Android. Offre inoltre una serie di strumenti per aiutarti a scrivere i test.
Ad esempio, AndroidX Test fornisce regole JUnit4 per avviare le attività e interagire con esse nei test JUnit4. Contiene inoltre framework di test dell'interfaccia utente come Espresso, UI Automator e simulatore Robolectric.
Aggiungere librerie di test AndroidX
Per utilizzare AndroidX Test, devi modificare le dipendenze del progetto dell'app all'interno dell'ambiente di sviluppo.
Aggiungi dipendenze Gradle
Per modificare le dipendenze del progetto dell'app, completa i seguenti passaggi:
- Passaggio 1: apri il file
build.gradle
per il modulo Gradle. - Passaggio 2: nella sezione dei repository, assicurati che venga visualizzato il repository Maven di Google:
allprojects {
repositories {
jcenter()
google()
}
}
- Passaggio 3: per ogni pacchetto di test AndroidX che vuoi utilizzare, aggiungi il nome del relativo pacchetto alla sezione delle dipendenze. Ad esempio, per aggiungere il pacchetto
espresso-core
, aggiungi le seguenti righe:
Trendy
dependencies { ... androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion" }
Kotlin
dependencies { ... androidTestImplementation('androidx.test.espresso:espresso-core:$espressoVersion') }
Di seguito sono riportate le dipendenze di AndroidX Test più comuni disponibili:
Trendy
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") }
La pagina Note di rilascio contiene una tabella con le versioni più recenti per ogni artefatto.
Fai riferimento all'indice dei pacchetti o all'indice delle classi per la documentazione di riferimento specifica su queste librerie.
Progetti che utilizzano classi deprecate
Se la tua app utilizza test che si basano su classi android.test
deprecate basate su JUnit3 , come InstrumentationTestCase
e TestSuiteLoader
, aggiungi
le seguenti righe nella sezione android
del file:
android {
...
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
}
Aggiungi dichiarazioni del file manifest
Per eseguire test basati sulle classi android.test
deprecate basate su JUnit3, aggiungi gli elementi <uses-library>
necessari al file manifest dell'app di test. Ad esempio, se aggiungi test che dipendono dalla libreria android.test.runner
, aggiungi il seguente elemento al file manifest dell'app:
<!-- 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" />
Per determinare la libreria che contiene una determinata classe basata su JUnit, consulta Librerie basate su JUnit.
Considerazioni sull'utilizzo di classi deprecate e sul targeting di Android 9 o
più alta
Le indicazioni riportate in questa sezione si applicano solo se scegli come target Android 9 (livello API 28) o versioni successive e la versione minima dell'SDK per la tua app è impostata su Android 9.
La libreria android.test.runner
dipende implicitamente dalle librerie android.test.base
e android.test.mock
. Se la tua app utilizza solo classi android.test.base
o android.test.mock
, puoi includere le librerie singolarmente:
<!-- 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" />