AndroidX Test to zbiór bibliotek Jetpack, które umożliwiają przeprowadzanie testów z aplikacjami na Androida. Udostępniamy też szereg narzędzi, które pomogą Ci w pisaniu 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 również platformy do testowania UI, „Espresso”, „UI Automator” i „Robolectric Simulator”.
Dodawanie bibliotek testowych AndroidaX
Aby używać Testu AndroidX, musisz zmodyfikować zależności projektu aplikacji w środowisku programistycznym.
Dodaj 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 narzędzie Google maven repozytorium pojawia się:
allprojects {
repositories {
jcenter()
google()
}
}
- Krok 3. Dodaj pakiet do każdego pakietu AndroidX Test, którego chcesz użyć
na
zależności. Aby na przykład dodać pakiet
espresso-core
, dodaj do w tych wierszach:
dependencies {
...
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
}
dependencies {
...
androidTestImplementation('androidx.test.espresso:espresso-core:$espressoVersion')
}
Oto najczęstsze zależności dostępne w narzędziu AndroidX Test:
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"
}
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 znajduje się tabela z najnowszymi wersjami artefaktu.
Szczegółowe informacje znajdziesz na stronach Package Index i Class Index. dokumentację tych bibliotek.
Projekty korzystające z wycofanych klas
Jeśli Twoja aplikacja korzysta z testów opartych na wycofanych interfejsach android.test
opartych na JUnit3
zajęć , 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'
}
Dodaj deklaracje w pliku manifestu
Aby przeprowadzić testy, które bazują na wycofanych klasach android.test
opartych na JUnit3, dodaj
niezbędnych elementów <uses-library>
w pliku manifestu aplikacji testowej. Dla:
Jeśli na przykład dodasz testy zależne od biblioteki android.test.runner
, dodaj
ten element do pliku manifestu aplikacji:
<!-- 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ę, która zawiera określoną klasę opartą na JUnit, zapoznaj się z artykułem Biblioteki oparte na JUnit.
O czym warto pamiętać w przypadku korzystania z wycofanych klas i kierowania na Androida 9 lub
wyższa
Wskazówki w tej sekcji obowiązują tylko w przypadku aplikacji kierowanych na Androida 9 (poziom API 28) lub nowszy oraz minimalna wersja pakietu SDK w Twojej aplikacji to Android 9.
Biblioteka android.test.runner
domyślnie zależy od funkcji android.test.base
i android.test.mock
. Jeśli Twoja aplikacja korzysta tylko z zajęć z
android.test.base
lub android.test.mock
, możesz uwzględnić biblioteki przez
się:
<!-- 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" />