AndroidX Test è una raccolta di librerie Jetpack che ti consente di eseguire test sulle app Android. Fornisce anche una serie di strumenti per aiutarti a scrivere questi test.
Ad esempio, AndroidX Test fornisce regole JUnit4 per avviare le attività e interagire con esse nei test JUnit4. Contiene anche framework di test dell'UI come Espresso, UI Automator e il simulatore Robolectric.
Aggiungere librerie AndroidX Test
Per utilizzare AndroidX Test, devi modificare le dipendenze del progetto dell'app all'interno dell'ambiente di sviluppo.
Aggiungere dipendenze Gradle
Per modificare le dipendenze del progetto dell'app, completa i seguenti passaggi:
- Passaggio 1: apri il file
build.gradleper 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 AndroidX Test che vuoi utilizzare, aggiungi il nome del pacchetto alla sezione delle dipendenze. Ad esempio, per aggiungere il pacchetto
espresso-core, aggiungi le seguenti righe:
Alla moda
dependencies { ... androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion" }
Kotlin
dependencies { ... androidTestImplementation('androidx.test.espresso:espresso-core:$espressoVersion') }
Di seguito sono riportate le dipendenze AndroidX Test più comuni disponibili:
Alla moda
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 artefatto.
Consulta l'indice dei pacchetti o l'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 android.test
classi basate su JUnit3 deprecate , 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'
}
Aggiungere dichiarazioni del manifest
Per eseguire test che si basano su classi basate su JUnit3 deprecate android.test, aggiungi
gli elementi <uses-library> necessari al manifest dell'app di test. Ad esempio, se aggiungi test che dipendono dalla libreria android.test.runner, aggiungi il seguente elemento al 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, vedi Librerie basate su JUnit.
Considerazioni sull'utilizzo di classi deprecate e sul targeting di Android 9 o
versioni successive
Le indicazioni in questa sezione si applicano solo se scegli come target Android 9 (livello API 28) o versioni successive e la versione SDK minima 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 di android.test.base o android.test.mock, puoi includere le librerie da sole:
<!-- 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" />