Skonfiguruj projekt na potrzeby Testu AndroidaX

AndroidX Test to zbiór bibliotek Jetpack, które umożliwiają przeprowadzanie testów aplikacji na Androida. Zawiera też serię narzędzi ułatwiających pisanie tych testów.

AndroidX Test udostępnia na przykład reguły JUnit4 do uruchamiania działań i interakcji z nimi w testach JUnit4. Zawiera też frameworki do testowania interfejsu użytkownika, takie jak Espresso, UI Automator i symulator Robolectric.

Dodawanie bibliotek AndroidX Test

Aby korzystać z AndroidX Test, musisz zmodyfikować zależności projektu aplikacji w środowisku programistycznym.

Dodawanie zależności Gradle

Aby zmodyfikować zależności projektu aplikacji, wykonaj te czynności:

  • Krok 1. Otwórz plik build.gradle modułu Gradle.
  • Krok 2. W sekcji repozytoriów sprawdź, czy znajduje się tam repozytorium Maven Google:
  allprojects {
    repositories {
      jcenter()
      google()
    }
  }
  • Krok 3. W sekcji zależności dodaj nazwę pakietu każdego pakietu AndroidX Test, którego chcesz używać. Aby na przykład dodać pakiet espresso-core, dodaj te wiersze:

Dynamiczny

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

Kotlin

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

Oto najczęstsze dostępne zależności AndroidX Test:

Dynamiczny

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

Na stronie Informacje o wersji znajdziesz tabelę z najnowszymi wersjami poszczególnych artefaktów.

Szczegółową dokumentację tych bibliotek znajdziesz w indeksie pakietów lub indeksie klas.

Projekty korzystające z wycofanych klas

Jeśli Twoja aplikacja używa testów, które opierają się na wycofanych klasach android.test opartych na JUnit3, takich jak InstrumentationTestCase i TestSuiteLoader, dodaj te wiersze w sekcji android pliku:

android {
    ...
    useLibrary 'android.test.runner'

    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
  }

Dodawanie deklaracji w pliku manifestu

Aby uruchamiać testy, które opierają się na wycofanych klasach opartych na JUnit3 android.test, dodaj niezbędne elementy <uses-library> do pliku manifestu aplikacji testowej. Jeśli na przykład dodasz testy, które zależą od biblioteki android.test.runner, dodaj do pliku manifestu aplikacji ten element:

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

Aby określić bibliotekę zawierającą daną klasę opartą na JUnit, zapoznaj się z sekcją Biblioteki oparte na JUnit.

O czym warto pamiętać podczas korzystania z wycofanych klas i kierowania aplikacji na Androida 9 lub

nowszego

Wskazówki w tej sekcji mają zastosowanie tylko wtedy, gdy kierujesz aplikację na Androida 9 (poziom API 28) lub nowszego oraz minimalna wersja SDK aplikacji jest ustawiona na Androida 9.

Biblioteka android.test.runner jest pośrednio zależna od bibliotek android.test.base i android.test.mock. Jeśli Twoja aplikacja używa tylko klas z bibliotek android.test.base lub android.test.mock, możesz uwzględnić te biblioteki samodzielnie:

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