Android Espresso – první jednoduchý test

Testujete? Já se o to pokouším. Pro komplexnější testování UI prvků aplikace doporučuji nástroj Espresso.

Pokud vytvoříte v Android Studiu nový projekt, máte základ připraven. Android Studio vytvoří základní strukturu a základní testy automaticky. Jedná se o složky:

  • androidTest – do této složky dáváme UI testy. Používáme: Espresso a JUnit
  • test – do teto složky dáváme Unit testy, pro tyto testy používáme: Mockito, Robolectric a JUnit

Naše Espresso testy tedy budeme umísťovat do složky androidTest. Pro první test stačí do souboru build.gradle (ve složce app) přidat závislost:

androidTestImplementation 'com.android.support.test:rules:1.0.2'

Můj build.gradle vypadá takto:

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "cz.vencax.hellobuild"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
}

Pojďme vytvořit náš první Espresso test. Otestujeme, zda TextView obsahuje text Hello World!

package cz.vencax.hellobuild;

import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.containsString;

@RunWith(AndroidJUnit4.class)
@LargeTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class FirstTest {

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);

@Before
public void setUp() throws Exception {
//Before Test case execution
}

@Test
public void test1ChatId() {
//onView(withId(R.id.helloTextView)).check(matches(isDisplayed()));
onView(withId(R.id.helloTextView)).check(matches(withText(containsString("Hello World!"))));
}

@After
public void tearDown() throws Exception {
//After Test case Execution
}
}

Test spustíme kliknutím pravým tlačítkem na soubor s testem v levé části Android Studia – Project. Nebo z příkazové řádky:

gradlew connectedAndroidTest