Configura progetto per AndroidX Test

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

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

Aggiungi librerie di test AndroidX

Per usare AndroidX Test, devi modificare le dipendenze del progetto di app all'interno del tuo 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 Repository, assicurati che Maven di Google viene visualizzato il repository:
  allprojects {
    repositories {
      jcenter()
      google()
    }
  }
  • Passaggio 3: aggiungi il pacchetto di ogni pacchetto AndroidX Test che vuoi utilizzare il nome del bucket delle dipendenze. Ad esempio, per aggiungere il pacchetto espresso-core, aggiungi il metodo seguenti righe:

Alla moda

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

Kotlin

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

Ecco le dipendenze di 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 artefatto.

Per riferimento specifico, consulta l'Indice pacchetti o l'Indice del corso. documentazione su queste librerie.

Progetti che utilizzano corsi deprecati

Se la tua app utilizza test che si basano su android.test basati su JUnit3 deprecati come InstrumentationTestCase e TestSuiteLoader, aggiungono 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 che si basano su classi android.test deprecate basate su JUnit3, aggiungi gli elementi <uses-library> necessari al file manifest dell'app di test. Per 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, vedi librerie basate su JUnit.

Considerazioni sull'utilizzo di corsi ritirati e sul targeting di Android 9 o

più alta

Le indicazioni in questa sezione si applicano solo se scegli come target Android 9 (livello API 28) o successiva e la versione minima dell'SDK per la tua app sia impostata su Android 9.

La libreria android.test.runner dipende implicitamente da android.test.base e android.test.mock. Se la tua app utilizza solo corsi da android.test.base o android.test.mock, puoi includere le raccolte tramite autonomamente:

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