AndroidX Test to zbiór bibliotek Jetpack, które umożliwiają przeprowadzanie testów z aplikacjami na Androida. Zapewnia też szereg narzędzi, które ułatwiają pisanie 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 UI, takie jak Espresso, Automator czy Robolectric.
Dodaj biblioteki testowe AndroidX
Aby korzystać z Androida Test, 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. Sprawdź, czy w sekcji repozytoriów znajduje się repozytorium Google Maven:
allprojects {
repositories {
jcenter()
google()
}
}
- Krok 3. W sekcji zależności dla każdego pakietu testów AndroidX, którego chcesz użyć, dodaj jego nazwę. Aby np. dodać pakiet
espresso-core
, dodaj te wiersze:
Odlotowy
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 w ramach testów AndroidX:
Odlotowy
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 znajduje się tabela z najnowszymi wersjami danego artefaktu.
Szczegółowe informacje o tych bibliotekach znajdziesz na stronach Package Index i Class Index.
Projekty korzystające z wycofanych zajęć
Jeśli Twoja aplikacja używa testów opartych na wycofanych klasach android.test
opartych na JUnit3 , takich jak InstrumentationTestCase
i TestSuiteLoader
, w sekcji android
pliku dodaj te wiersze:
android {
...
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
}
Dodaj deklaracje w pliku manifestu
Aby przeprowadzać testy oparte na wycofanych klasach android.test
opartych na JUnit3, dodaj niezbędne elementy <uses-library>
do pliku manifestu aplikacji testowej. Jeśli na przykład dodasz testy zależne 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ę, która zawiera daną klasę opartą na JUnit, przeczytaj sekcję o bibliotekach opartych na JUnit.
Uwagi do rozważenia w przypadku korzystania z wycofanych klas i kierowania na Androida 9 lub
wyższa
Wskazówki w tej sekcji mają zastosowanie tylko wtedy, gdy kierujesz aplikację na Androida 9 (poziom interfejsu API 28) lub nowszego, a minimalną wersję pakietu SDK dla aplikacji ustawiono na Androida 9.
Biblioteka android.test.runner
domyślnie zależy od bibliotek android.test.base
i android.test.mock
. Jeśli Twoja aplikacja używa tylko klas z android.test.base
lub android.test.mock
, możesz samodzielnie uwzględnić te biblioteki:
<!-- 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" />