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:
1 |
androidTestImplementation <strong>'com.android.support.test:rules:1.0.2'</strong><br> |
Můj build.gradle vypadá takto:
1 |
apply <strong>plugin</strong>: <strong>'com.android.application'<br></strong><strong><br></strong>android {<br> compileSdkVersion 28<br> defaultConfig {<br> applicationId <strong>"cz.vencax.hellobuild"<br></strong><strong> </strong>minSdkVersion 15<br> targetSdkVersion 28<br> versionCode 1<br> versionName <strong>"1.0"<br></strong><strong> </strong>testInstrumentationRunner <strong>"android.support.test.runner.AndroidJUnitRunner"<br></strong><strong> </strong>}<br> buildTypes {<br> release {<br> minifyEnabled <strong>false<br></strong><strong> </strong>proguardFiles getDefaultProguardFile(<strong>'proguard-android-optimize.txt'</strong>), <strong>'proguard-rules.pro'<br></strong><strong> </strong>}<br> }<br>}<br><br>dependencies {<br> implementation fileTree(<strong>include</strong>: [<strong>'*.jar'</strong>], <strong>dir</strong>: <strong>'libs'</strong>)<br> implementation <strong>'com.android.support:appcompat-v7:28.0.0'<br></strong><strong> </strong>implementation <strong>'com.android.support.constraint:constraint-layout:1.1.3'<br></strong><strong> </strong>testImplementation <strong>'junit:junit:4.12'<br></strong><strong> </strong>androidTestImplementation <strong>'com.android.support.test:runner:1.0.2'<br></strong><strong> </strong>androidTestImplementation <strong>'com.android.support.test.espresso:espresso-core:3.0.2'<br></strong><strong> </strong>androidTestImplementation <strong>'com.android.support.test:rules:1.0.2'<br></strong>}<br> |
Pojďme vytvořit náš první Espresso test. Otestujeme, zda TextView obsahuje text Hello World!
1 |
<strong>package </strong>cz.vencax.hellobuild;<br><br><strong>import </strong>android.support.test.filters.LargeTest;<br><strong>import </strong>android.support.test.rule.ActivityTestRule;<br><strong>import </strong>android.support.test.runner.AndroidJUnit4;<br><br><strong>import </strong>org.junit.After;<br><strong>import </strong>org.junit.Before;<br><strong>import </strong>org.junit.FixMethodOrder;<br><strong>import </strong>org.junit.Rule;<br><strong>import </strong>org.junit.Test;<br><strong>import </strong>org.junit.runner.RunWith;<br><strong>import </strong>org.junit.runners.MethodSorters;<br><br><strong>import static </strong>android.support.test.espresso.Espresso.<em>onView</em>;<br><strong>import static </strong>android.support.test.espresso.assertion.ViewAssertions.<em>matches</em>;<br><strong>import static </strong>android.support.test.espresso.matcher.ViewMatchers.<em>isDisplayed</em>;<br><strong>import static </strong>android.support.test.espresso.matcher.ViewMatchers.<em>withId</em>;<br><strong>import static </strong>android.support.test.espresso.matcher.ViewMatchers.<em>withText</em>;<br><strong>import static </strong>org.hamcrest.Matchers.<em>containsString</em>;<br><br>@RunWith(AndroidJUnit4.<strong>class</strong>)<br>@LargeTest<br>@FixMethodOrder(MethodSorters.<strong><em>NAME_ASCENDING</em></strong>)<br><strong>public class </strong>FirstTest {<br><br> @Rule<br> <strong>public </strong>ActivityTestRule<MainActivity> <strong>mActivityRule </strong>= <strong>new </strong>ActivityTestRule<>(MainActivity.<strong>class</strong>);<br><br> @Before<br> <strong>public void </strong>setUp() <strong>throws </strong>Exception {<br> <em>//Before Test case execution<br> </em>}<br><br> @Test<br> <strong>public void </strong>test1ChatId() {<br> //<em>onView</em>(<em>withId</em>(R.id.<strong><em>helloTextView</em></strong>)).check(<em>matches</em>(<em>isDisplayed</em>()));<br> <em>onView</em>(<em>withId</em>(R.id.<strong><em>helloTextView</em></strong>)).check(<em>matches</em>(<em>withText</em>(<em>containsString</em>(<strong>"Hello World!"</strong>))));<br> }<br><br> @After<br> <strong>public void </strong>tearDown() <strong>throws </strong>Exception {<br> <em>//After Test case Execution<br> </em>}<br>} |
Test spustíme kliknutím pravým tlačítkem na soubor s testem v levé části Android Studia – Project. Nebo z příkazové řádky:
1 |
gradlew connectedAndroidTest |