Skonfiguruj projekt na potrzeby Testu AndroidaX

AndroidX Test to zbiór bibliotek Jetpack, które umożliwiają przeprowadzanie testów aplikacji na Androida. Udostępnia też szereg narzędzi, które pomagają w pisaniu tych testów.

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

Dodawanie bibliotek testowych AndroidX

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 jest widoczne repozytorium Maven od Google:
  allprojects {
    repositories {
      jcenter()
      google()
    }
  }
  • Krok 3. W sekcji dependencies dodaj nazwę pakietu każdego pakietu testowego AndroidX, którego chcesz używać. Aby na przykład dodać pakiet espresso-core, dodaj te wiersze:

Groovy

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:

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

Strona Informacje o wersji zawiera tabelę z najnowszymi wersjami poszczególnych artefaktów.

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

Projekty korzystające z wycofanych klas

Jeśli Twoja aplikacja korzysta z testów opartych na wycofanych klasach JUnit3 , takich jak android.testInstrumentationTestCaseTestSuiteLoader, 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 korzystają z wycofanych klas android.test opartych na JUnit3, dodaj do pliku manifestu aplikacji testowej niezbędne elementy <uses-library>. 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 na Androida 9 lub nowszego

wyższa

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

Biblioteka android.test.runner jest pośrednio zależna od bibliotek android.test.baseandroid.test.mock. Jeśli Twoja aplikacja używa tylko klas z android.test.base lub android.test.mock, możesz uwzględnić 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" />