Questa sezione fornisce una panoramica di cosa sono i test Android e perché uno sviluppatore potrebbe volerlo utilizzare.
Dovrebbe anche menzionare qualsiasi argomento di grandi dimensioni nei test di Android e collegarsi agli argomenti correlati. Poiché la documentazione per Android-testing è nuova, potrebbe essere necessario creare versioni iniziali di tali argomenti correlati.
I test Android sono basati su JUnit e puoi eseguirli sia come test delle unità locali sulla JVM che come test strumentati su un dispositivo Android. Questa pagina fornisce un'introduzione ai concetti e agli strumenti per la creazione di test Android
Test di unità locali JUnit
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityInstrumentationTest {
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule<>(
MainActivity.class);
@Test
public void sayHello(){
onView(withText("Say hello!")).perform(click());
onView(withId(R.id.textView)).check(matches(withText("Hello, World!")));
}
}
Test strumentati Esempio di dipendenze {androidTestCompile 'com.android.support:support-annotations:24.0.0' androidTestCompile 'com.android.support.test: runner: 0.5' androidTestCompile 'com.android.support.test: rules: 0.5' / / Facoltativo - Libreria Hamcrest androidTestCompile 'org.hamcrest: hamcrest-library: 1.3' // Opzionale - Test dell'interfaccia utente con Espresso androidTestCompile 'com.android.support.test.espresso: espresso-core: 2.2.2' // Opzionale - Test dell'interfaccia utente con UI Automator androidTestCompile 'com.android.support.test.uiautomator: uiautomator-v18: 2.1.2'}
android {defaultConfig {testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}}
Classe di test per unità strumentate
import android.os.Parcel;
import android.support.test.runner.AndroidJUnit4;
import android.util.Pair;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@RunWith(AndroidJUnit4.class)
@SmallTest
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(), is(1));
assertThat(createdFromParcelData.get(0).first, is(TEST_STRING));
assertThat(createdFromParcelData.get(0).second, is(TEST_LONG));
}
}