1page.title=Building Instrumented Unit Tests 2page.tags=testing,androidjunitrunner,junit,unit test,mock,instrumentation 3trainingnavtop=true 4 5@jd:body 6 7<!-- This is the training bar --> 8<div id="tb-wrapper"> 9<div id="tb"> 10 <h2>This lesson teaches you to</h2> 11 12 <ol> 13 <li><a href="#setup">Set Up Your Testing Environment</a></li> 14 <li><a href="#build">Create a Instrumented Unit Test Class</a> 15 <ol> 16 <li><a href="#test-suites">Create a test suite</a></li> 17 </ol> 18 </li> 19 <li><a href="#run">Run Instrumented Unit Tests</a> 20 <ol> 21 <li><a href="#run-ctl">Run your tests with Firebase Test Lab</a></li> 22 </ol> 23 </li> 24 </ol> 25 26 <h2>Try it out</h2> 27 28 <ul> 29 <li> 30<a href="https://github.com/googlesamples/android-testing/tree/master/unit/BasicUnitAndroidTest" 31class="external-link">Instrumented Unit Tests Code Samples</a></li> 32 <li><a href="https://www.code-labs.io/codelabs/android-studio-testing/index.html?index=..%2F..%2Findex#0" 33class="external-link">Unit and UI Testing in Android Studio (codelab)</a></li> 34 </ul> 35</div> 36</div> 37 38<p>Instrumented unit tests are tests that run on physical devices and 39emulators, and they can take advantage of the Android framework APIs and 40supporting APIs, such as the Android Testing Support Library. You should create 41instrumented unit tests if your tests need access to instrumentation 42information (such as the target app's {@link android.content.Context}) or if 43they require the real implementation of an Android framework component (such as 44a {@link android.os.Parcelable} or {@link android.content.SharedPreferences} 45object).</p> 46 47<p>Using instrumented unit tests also helps to reduce the effort required to 48write and maintain mock code. You are still free to use a mocking framework, if 49you choose, to simulate any dependency relationships.</p> 50 51 52<h2 id="setup">Set Up Your Testing Environment</h2> 53 54<p>In your Android Studio project, you must store the source files for 55instrumented tests at 56<code><var>module-name</var>/src/androidTests/java/</code>. This directory 57already exists when you create a new project.</p> 58 59<p>Before you begin, you should 60 <a href="{@docRoot}tools/testing-support-library/index.html#setup">download 61 the Android Testing Support Library Setup</a>, which provides APIs that allow 62 you to quickly build and run instrumented test code for your apps. The 63 Testing Support Library includes a JUnit 4 test runner (<a href= 64 "{@docRoot}tools/testing-support-library/index.html#AndroidJUnitRunner">AndroidJUnitRunner</a> 65 ) and APIs for functional UI tests (<a href= 66 "{@docRoot}tools/testing-support-library/index.html#Espresso">Espresso</a> 67 and <a href= 68 "{@docRoot}tools/testing-support-library/index.html#UIAutomator">UI 69 Automator</a>). 70</p> 71 72<p>You also need to configure the Android testing dependencies for your project 73to use the test runner and the rules APIs provided by the Testing Support 74Library. To simplify your test development, you should also include the 75<a href="https://github.com/hamcrest" class="external-link">Hamcrest</a> 76library, which lets you create more flexible assertions using the Hamcrest 77matcher APIs.</p> 78 79<p> 80 In your app's top-level {@code build.gradle} file, you need to specify these 81 libraries as dependencies: 82</p> 83 84<pre> 85dependencies { 86 androidTestCompile 'com.android.support:support-annotations:24.0.0' 87 androidTestCompile 'com.android.support.test:runner:0.5' 88 androidTestCompile 'com.android.support.test:rules:0.5' 89 // Optional -- Hamcrest library 90 androidTestCompile 'org.hamcrest:hamcrest-library:1.3' 91 // Optional -- UI testing with Espresso 92 androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' 93 // Optional -- UI testing with UI Automator 94 androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' 95} 96</pre> 97 98<div class="caution"> 99<p><strong>Caution:</strong> If your build configuration includes a 100<code>compile</code> dependency for the <code>support-annotations</code> 101library <b>and</b> an <code>androidTestCompile</code> dependency for the 102<code>espresso-core</code> library, your build might fail due to a dependency 103conflict. To resolve, update your dependency for <code>espresso-core</code> 104as follows:</p> 105<pre> 106androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 107 exclude group: 'com.android.support', module: 'support-annotations' 108}) 109</pre> 110</div> 111 112<p> 113 To use JUnit 4 test classes, make sure to specify <a href= 114 "{@docRoot}reference/android/support/test/runner/AndroidJUnitRunner.html">{@code 115 AndroidJUnitRunner}</a> as the default test instrumentation runner in your 116 project by including the following setting in your app's module-level {@code build.gradle} 117 file: 118</p> 119 120<pre> 121android { 122 defaultConfig { 123 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 124 } 125} 126</pre> 127 128 129 130 131<h2 id="build">Create an Instrumented Unit Test Class</h2> 132 133<p> 134Your instrumented unit test class should be written as a JUnit 4 test class. To learn more about 135creating JUnit 4 test classes and using JUnit 4 assertions and annotations, see 136<a href="local-unit-tests.html#build">Create a Local Unit Test Class</a>. 137</p> 138<p>To create an instrumented JUnit 4 test class, add the {@code @RunWith(AndroidJUnit4.class)} 139annotation at the beginning of your test class definition. You also need to specify the 140<a href="{@docRoot}reference/android/support/test/runner/AndroidJUnitRunner.html"> 141{@code AndroidJUnitRunner}</a> class 142provided in the Android Testing Support Library as your default test runner. This step is described 143in more detail in <a href="{@docRoot}training/testing/start/index.html#run-instrumented-tests"> 144Getting Started with Testing</p> 145 146<p>The following example shows how you might write an instrumented unit test to test that 147the {@link android.os.Parcelable} interface is implemented correctly for the 148{@code LogHistory} class:</p> 149 150<pre> 151import android.os.Parcel; 152import android.support.test.runner.AndroidJUnit4; 153import android.util.Pair; 154import org.junit.Test; 155import org.junit.runner.RunWith; 156import java.util.List; 157import static org.hamcrest.Matchers.is; 158import static org.junit.Assert.assertThat; 159 160@RunWith(AndroidJUnit4.class) 161@SmallTest 162public class LogHistoryAndroidUnitTest { 163 164 public static final String TEST_STRING = "This is a string"; 165 public static final long TEST_LONG = 12345678L; 166 private LogHistory mLogHistory; 167 168 @Before 169 public void createLogHistory() { 170 mLogHistory = new LogHistory(); 171 } 172 173 @Test 174 public void logHistory_ParcelableWriteRead() { 175 // Set up the Parcelable object to send and receive. 176 mLogHistory.addEntry(TEST_STRING, TEST_LONG); 177 178 // Write the data. 179 Parcel parcel = Parcel.obtain(); 180 mLogHistory.writeToParcel(parcel, mLogHistory.describeContents()); 181 182 // After you're done with writing, you need to reset the parcel for reading. 183 parcel.setDataPosition(0); 184 185 // Read the data. 186 LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel); 187 List<Pair<String, Long>> createdFromParcelData = createdFromParcel.getData(); 188 189 // Verify that the received data is correct. 190 assertThat(createdFromParcelData.size(), is(1)); 191 assertThat(createdFromParcelData.get(0).first, is(TEST_STRING)); 192 assertThat(createdFromParcelData.get(0).second, is(TEST_LONG)); 193 } 194} 195</pre> 196 197<h3 id="test-suites">Create a test suite</h3> 198<p> 199To organize the execution of your instrumented unit tests, you can group a collection of test 200classes in a <em>test suite</em> class and run these tests together. Test suites can be nested; 201your test suite can group other test suites and run all their component test classes together. 202</p> 203 204<p> 205A test suite is contained in a test package, similar to the main application package. By 206convention, the test suite package name usually ends with the {@code .suite} suffix (for example, 207{@code com.example.android.testing.mysample.suite}). 208</p> 209 210<p> 211To create a test suite for your unit tests, import the JUnit 212<a href="http://junit.sourceforge.net/javadoc/org/junit/runner/RunWith.html" 213class="external-link">{@code RunWith}</a> and 214<a href="http://junit.sourceforge.net/javadoc/org/junit/runners/Suite.html" 215class="external-link">{@code Suite}</a> classes. In your test suite, add the 216{@code @RunWith(Suite.class)} and the {@code @Suite.SuitClasses()} annotations. In 217the {@code @Suite.SuiteClasses()} annotation, list the individual test classes or test 218suites as arguments. 219</p> 220 221<p> 222The following example shows how you might implement a test suite called {@code UnitTestSuite} 223that groups and runs the {@code CalculatorInstrumentationTest} and 224{@code CalculatorAddParameterizedTest} test classes together. 225</p> 226 227<pre> 228import com.example.android.testing.mysample.CalculatorAddParameterizedTest; 229import com.example.android.testing.mysample.CalculatorInstrumentationTest; 230import org.junit.runner.RunWith; 231import org.junit.runners.Suite; 232 233// Runs all unit tests. 234@RunWith(Suite.class) 235@Suite.SuiteClasses({CalculatorInstrumentationTest.class, 236 CalculatorAddParameterizedTest.class}) 237public class UnitTestSuite {} 238</pre> 239 240 241<h2 id="run">Run Instrumented Unit Tests</h2> 242 243<p>To run your instrumented tests, follow these steps:</p> 244 245<ol> 246 <li>Be sure your project is synchronized with Gradle by clicking 247 <b>Sync Project</b> <img src="/images/tools/sync-project.png" alt="" 248 class="inline-icon"> in the toolbar.</li> 249 250 <li>Run your test in one of the following ways: 251 <ul> 252 <li>To run a single test, open the <b>Project</b> window, and then 253 right-click a test and click <strong>Run</strong> <img src= 254 "{@docRoot}images/tools/as-run.png" alt="" class="inline-icon">.</li> 255 <li>To test all methods in a class, right-click a class or method in the 256test file and click <b>Run</b> <img src= 257 "{@docRoot}images/tools/as-run.png" alt="" class="inline-icon">. 258 <li>To run all tests in a directory, right-click on the 259 directory and select <strong>Run tests</strong> <img src= 260 "{@docRoot}images/tools/as-run.png" alt="" class="inline-icon">. 261 </li> 262 </ul> 263 </li> 264 265</ol> 266 267<p> 268 The <a href="{@docRoot}tools/building/plugin-for-gradle.html">Android Plugin 269 for Gradle</a> compiles the instrumented test code located in the default 270 directory ({@code src/androidTest/java/}), builds a test APK and production 271 APK, installs both APKs on the connected device or emulator, and runs the 272 tests. Android Studio then displays the results of the instrumented test execution in the 273 <em>Run</em> window. 274</p> 275 276<p class="note"> 277 <strong>Note:</strong> While running or debugging instrumented tests, 278 Android Studio does not inject the additional methods required for <a href= 279 "{@docRoot}tools/building/building-studio.html#instant-run">Instant Run</a> 280 and turns the feature off. 281</p> 282 283 284<h3 id="run-ctl">Run your tests with Firebase Test Lab</h3> 285 286<p>Using <a href="https://firebase.google.com/docs/test-lab/">Firebase Test 287Lab</a>, you can simultaneously test your app on many popular Android devices 288and configurations such as locale, orientation, screen size, and platform 289version. These tests run on actual physical devices in remote Google data 290centers. You can deploy to Firebase Test Lab directly from Android Studio or 291from the command line. Test results provide test logs and include the details 292of any app failures.</p> 293 294<p> 295 Before you can start using Firebase Test Lab, you need to: 296</p> 297 298<ol> 299 <li> 300 <a href="https://console.developers.google.com/freetrial">Create a 301 Google Cloud Platform account</a> to use with active billing. 302 </li> 303 304 <li> 305 <a href="https://support.google.com/cloud/answer/6251787">Create a Google 306 Cloud project</a> for your app. 307 </li> 308 309 <li> 310 <a href="https://support.google.com/cloud/answer/6288653">Set up an active 311 billing account</a> and associate it with the project you just created. 312 </li> 313</ol> 314 315 316<h4 id="configure-matrix"> 317Configure a test matrix and run a test 318</h4> 319 320<p> 321 Android Studio provides integrated tools that allow you to configure how you 322 want to deploy your tests to Firebase Test Lab. After you have created a Google 323 Cloud project with active billing, you can create a test configuration and 324 run your tests: 325</p> 326 327<ol> 328 <li>Click <strong>Run</strong> > <strong>Edit Configurations</strong> from 329 the main menu. 330 </li> 331 332 <li>Click <strong>Add New Configuration (+)</strong> and select 333 <strong>Android Tests</strong>. 334 </li> 335 336 <li>In the Android Test configuration dialog: 337 <ol type="a"> 338 <li>Enter or select the details of your test, such as the test name, module 339 type, test type, and test class. 340 </li> 341 342 <li>From the <em>Target</em> drop-down menu under <em>Deployment Target 343 Options</em>, select <strong>Cloud Test Lab Device Matrix</strong>. 344 </li> 345 346 <li>If you are not logged in, click <strong>Connect to Google Cloud 347 Platform</strong> and allow Android Studio access to your account. 348 </li> 349 350 <li>Next to <em>Cloud Project</em>, click the <img src= 351 "{@docRoot}images/tools/as-wrench.png" alt="wrench and nut" style= 352 "vertical-align:bottom;margin:0;"> button and select your Google Cloud 353 Platform project from the list. 354 </li> 355 </ol> 356 </li> 357 358 <li>Create and configure a test matrix: 359 <ol type="a"> 360 <li>Next to the <em>Matrix Configuration</em> drop-down list, click <strong> 361 Open Dialog</strong> <img src="{@docRoot}images/tools/as-launchavdm.png" 362 alt="ellipses button" style="vertical-align:bottom;margin:0;">. 363 </li> 364 365 <li>Click <strong>Add New Configuration (+)</strong>. 366 </li> 367 368 <li>In the <strong>Name</strong> field, enter a name for your new 369 configuration. 370 </li> 371 372 <li>Select the device(s), Android version(s), locale(s) and screen 373 orientation(s) that you want to test your app with. Firebase Test Lab will 374 test your app against every combination of your selections when generating 375 test results. 376 </li> 377 378 <li>Click <strong>OK</strong> to save your configuration. 379 </li> 380 </ol> 381 </li> 382 383 <li>Click <strong>OK</strong> in the <em>Run/Debug Configurations</em> dialog 384 to exit. 385 </li> 386 387 <li>Run your tests by clicking <strong>Run</strong> <img src= 388 "{@docRoot}images/tools/as-run.png" alt="" style= 389 "vertical-align:bottom;margin:0;">. 390 </li> 391</ol> 392 393<img src="{@docRoot}images/training/ctl-config.png" alt=""> 394<p class="img-caption"> 395 <strong>Figure 1.</strong> Creating a test configuration for Firebase Test 396 Lab. 397</p> 398 399<h4 id="ctl-results"> 400 Analyzing test results 401</h4> 402 403<p> 404 When Firebase Test Lab completes running your tests, the <em>Run</em> window 405 will open to show the results, as shown in figure 2. You may need to click 406 <strong>Show Passed</strong> <img src="{@docRoot}images/tools/as-ok.png" alt= 407 "" style="vertical-align:bottom;margin:0;"> to see all your executed tests. 408</p> 409 410<img src="{@docRoot}images/training/ctl-test-results.png" alt=""> 411 412<p class="img-caption"> 413 <strong>Figure 2.</strong> Viewing the results of instrumented tests using 414 Firebase Test Lab. 415</p> 416 417<p> 418 You can also analyze your tests on the web by following the link displayed at 419 the beginning of the test execution log in the <em>Run</em> window, as shown 420 in figure 3. 421</p> 422 423<img src="{@docRoot}images/training/ctl-exec-log.png" alt=""> 424 425<p class="img-caption"> 426 <strong>Figure 3.</strong> Click the link to view detailed test results on 427 the web. 428</p> 429 430<p> 431 To learn more about interpreting web results, see <a href= 432 "https://firebase.google.com/docs/test-lab/analyzing-results">Analyze 433 Firebase Test Lab for Android Results</a>. 434</p>