Testy instrumentowane są przeprowadzane na urządzeniach z Androidem, zarówno fizycznych, jak i emulowanych. Dzięki temu mogą korzystać z interfejsów API platformy Androida. Testy instrumentowane są więc bardziej wiarygodne niż testy lokalne, ale działają znacznie wolniej.
Zalecamy używanie testów z instrumentacją tylko wtedy, gdy musisz przetestować zachowanie rzeczywistego urządzenia. AndroidX Test udostępnia kilka bibliotek, które ułatwiają pisanie testów z instrumentacją, gdy jest to konieczne.
Konfigurowanie środowiska testowego
W projekcie Android Studio pliki źródłowe testów z instrumentacją są przechowywane w module-name/src/androidTest/java/. Ten katalog istnieje już podczas tworzenia nowego projektu i zawiera przykładowy test z instrumentacją.
Zanim zaczniesz, dodaj interfejsy AndroidX Test API, które umożliwiają szybkie tworzenie i uruchamianie kodu testów z instrumentacją dla aplikacji. AndroidX Test zawiera m.in. narzędzie do uruchamiania testów JUnit 4 (AndroidJUnitRunner) oraz interfejsy API do testów funkcjonalnych interfejsu, takie jak Espresso, UI Automator i Compose test.
Musisz też skonfigurować zależności testowania Androida w projekcie, aby używać modułu uruchamiającego testy i interfejsów API reguł udostępnianych przez AndroidX Test.
W pliku build.gradle najwyższego poziomu aplikacji musisz określić te biblioteki jako zależności:
dependencies {
androidTestImplementation "androidx.test:runner:$androidXTestVersion"
androidTestImplementation "androidx.test:rules:$androidXTestVersion"
// Optional -- UI testing with Espresso
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
// Optional -- UI testing with UI Automator
androidTestImplementation "androidx.test.uiautomator:uiautomator:$uiAutomatorVersion"
// Optional -- UI testing with Compose
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
}
Najnowsze wersje znajdziesz w informacjach o wersji AndroidX i informacjach o wersji interfejsu Compose.
Aby używać klas testowych JUnit 4 i mieć dostęp do funkcji takich jak filtrowanie testów, musisz określić AndroidJUnitRunner jako domyślny moduł uruchamiający instrumentację testów w projekcie, dodając to ustawienie w pliku build.gradle na poziomie modułu aplikacji:
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
Tworzenie klasy testu z instrumentacją
Klasa testu z instrumentacją powinna być klasą testową JUnit 4 podobną do klasy opisanej w sekcji poświęconej tworzeniu testów lokalnych.
Aby utworzyć klasę testową JUnit 4, określ AndroidJUnit4 jako domyślny moduł uruchamiający testy.
Poniższy przykład pokazuje, jak napisać test z instrumentacją, aby sprawdzić, czy interfejs Parcelable jest prawidłowo zaimplementowany w klasie LogHistory:
Kotlin
import android.os.Parcel import android.text.TextUtils.writeToParcel import androidx.test.filters.SmallTest import androidx.test.runner.AndroidJUnit4 import com.google.common.truth.Truth.assertThat import org.junit.Before import org.junit.Test import org.junit.runner.RunWith const val TEST_STRING = "This is a string" const val TEST_LONG = 12345678L // @RunWith is required only if you use a mix of JUnit3 and JUnit4. @RunWith(AndroidJUnit4::class) @SmallTest class LogHistoryAndroidUnitTest { private lateinit var logHistory: LogHistory @Before fun createLogHistory() { logHistory = LogHistory() } @Test fun logHistory_ParcelableWriteRead() { val parcel = Parcel.obtain() logHistory.apply { // Set up the Parcelable object to send and receive. addEntry(TEST_STRING, TEST_LONG) // Write the data. writeToParcel(parcel, describeContents()) } // After you're done with writing, you need to reset the parcel for reading. parcel.setDataPosition(0) // Read the data. val createdFromParcel: LogHistory = LogHistory.CREATOR.createFromParcel(parcel) createdFromParcel.getData().also { createdFromParcelData: List<Pair<String, Long>> -> // Verify that the received data is correct. assertThat(createdFromParcelData.size).isEqualTo(1) assertThat(createdFromParcelData[0].first).isEqualTo(TEST_STRING) assertThat(createdFromParcelData[0].second).isEqualTo(TEST_LONG) } } }
Java
import android.os.Parcel; import android.util.Pair; import androidx.test.runner.AndroidJUnit4; import com.google.common.truth.Truth.assertThat; import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; // @RunWith is required only if you use a mix of JUnit3 and JUnit4. @RunWith(AndroidJUnit4.class) public class LogHistoryAndroidUnitTest { public static final String TEST_STRING = "This is a string"; public static final long TEST_LONG = 12345678L; private LogHistory mLogHistory; @Before public void createLogHistory() { mLogHistory = new LogHistory(); } @Test public void logHistory_ParcelableWriteRead() { // Set up the Parcelable object to send and receive. mLogHistory.addEntry(TEST_STRING, TEST_LONG); // Write the data. Parcel parcel = Parcel.obtain(); mLogHistory.writeToParcel(parcel, mLogHistory.describeContents()); // After you're done with writing, you need to reset the parcel for reading. parcel.setDataPosition(0); // Read the data. LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel); List<Pair<String, Long>> createdFromParcelData = createdFromParcel.getData(); // Verify that the received data is correct. assertThat(createdFromParcelData.size()).isEqualTo(1); assertThat(createdFromParcelData.get(0).first).isEqualTo(TEST_STRING); assertThat(createdFromParcelData.get(0).second).isEqaulTo(TEST_LONG); } }
Przeprowadzanie testów z instrumentacją
Testy instrumentowane można przeprowadzać na rzeczywistych urządzeniach lub emulatorach. Z przewodnika po Android Studio dowiesz się, jak:
Dodatkowe materiały
Testy interfejsu to zwykle testy instrumentowane, które sprawdzają, czy interfejs działa prawidłowo. Używają platform takich jak Espresso lub Compose Test. Więcej informacji znajdziesz w przewodniku po testowaniu interfejsu.
Więcej informacji o korzystaniu z testów z instrumentacją znajdziesz w tych materiałach.