Configura progetto per AndroidX Test

AndroidX Test è una raccolta di librerie Jetpack che ti consente di eseguire test sulle app per Android. Fornisce inoltre una serie di strumenti per aiutarti a scrivere questi test.

Ad esempio, AndroidX Test fornisce regole JUnit4 per avviare attività e interagire con loro nei test JUnit4. Contiene anche framework di test dell'interfaccia utente come Espresso, UI Automator e il simulatore Robolectric.

Aggiungere librerie di test AndroidX

Per utilizzare AndroidX Test, devi modificare le dipendenze del progetto dell'app all'interno del tuo ambiente di sviluppo.

Aggiungere 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 pacchetto alla sezione dependencies. Ad esempio, per aggiungere il pacchetto espresso-core, aggiungi le seguenti righe:

Groovy

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:

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")
}

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 classi android.test 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 android.test basate su JUnit3 obsolete, 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 della tua 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 la sezione Librerie basate su JUnit.

Considerazioni sull'utilizzo di classi obsolete 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 di android.test.base o android.test.mock, puoi includere le librerie stesse:

<!-- 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" />