blob: 74617b0cc64c7619a13d70fa9d1da0df1983b3a3 [file] [log] [blame]
Quddus Chonge3f6c812015-08-26 14:16:05 -07001page.title=Getting Started with Testing
2page.tags="testing"
3page.article=true
4page.image=images/tools/studio-main-screen.png
5
6@jd:body
7
8<div id="tb-wrapper">
Adarsh Fernando44eb11a2016-03-29 12:56:50 -07009 <div id="tb">
Quddus Chonge3f6c812015-08-26 14:16:05 -070010
Adarsh Fernando44eb11a2016-03-29 12:56:50 -070011 <h2>
12 Dependencies and prerequisites
13 </h2>
Quddus Chonge3f6c812015-08-26 14:16:05 -070014
Adarsh Fernando44eb11a2016-03-29 12:56:50 -070015 <ul>
16 <li>
17 <a href="{@docRoot}tools/studio/index.html">Android Studio 2.0</a>, or
18 later.
19 </li>
Quddus Chonge3f6c812015-08-26 14:16:05 -070020
Adarsh Fernando44eb11a2016-03-29 12:56:50 -070021 <li>The Android Support Repository (available from the <a href=
22 "{@docRoot}tools/help/sdk-manager.html">SDK Manager</a>)
23 </li>
24 </ul>
Quddus Chonge3f6c812015-08-26 14:16:05 -070025
Adarsh Fernando44eb11a2016-03-29 12:56:50 -070026 <h2>
27 This lesson teaches you to
28 </h2>
29
30 <ol>
31 <li>
32 <a href="#config-local-tests">Configure Your Project for Local Unit
33 Tests</a>
34 </li>
35
36 <li>
37 <a href="#config-instrumented-tests">Configure Your Project for
38 Instrumented Tests</a>
39 </li>
40
41 <li>
42 <a href="#build">Build and Run Your Tests</a>
43 <ol>
44 <li>
45 <a href="#run-local-tests">Run Local Unit Tests</a>
46 </li>
47
48 <li>
49 <a href="#run-instrumented-tests">Run Instrumented Tests</a>
50 </li>
51
52 <li>
53 <a href="#run-ctl">Run Instrumented Tests with Cloud Test Lab</a>
54 </li>
55 </ol>
56 </li>
57 </ol>
58
59 <h2>
60 See also
61 </h2>
62
63 <ul>
64 <li>
65 <a href="{@docRoot}tools/testing/testing_android.html">Testing
66 Concepts</a>
67 </li>
68
69 <li>
70 <a href="https://github.com/googlesamples/android-testing" class=
71 "external-link">Android Testing Samples</a>
72 </li>
73
74 <li>
75 <a href="https://developers.google.com/cloud-test-lab/">Cloud Test
76 Lab</a>
77 </li>
78 </ul>
79 </div>
Quddus Chonge3f6c812015-08-26 14:16:05 -070080</div>
81
Adarsh Fernando44eb11a2016-03-29 12:56:50 -070082<p>
83 Writing and running tests are important parts of the Android app development
84 cycle. Well-written tests can help you catch bugs early in development and
85 give you confidence in your code. Using Android Studio, you can run local
86 unit tests or instrumented tests on a variety of physical or virtual Android
87 devices. You can then analyze the results and make changes to your code
88 without leaving the development environment.
89</p>
Quddus Chonge3f6c812015-08-26 14:16:05 -070090
Adarsh Fernando44eb11a2016-03-29 12:56:50 -070091<p>
92 <em>Local unit tests</em> are tests that run on your local machine, without
93 needing access to the Android framework or an Android device. To learn how to
94 develop local units tests, see <a href=
95 "{@docRoot}training/testing/unit-testing/local-unit-tests.html">Building
96 Local Unit Tests</a>.
97</p>
Quddus Chonge3f6c812015-08-26 14:16:05 -070098
Adarsh Fernando44eb11a2016-03-29 12:56:50 -070099<p>
100 <em>Instrumented tests</em> are tests that run on an Android device or
101 emulator. These tests have access to {@link android.app.Instrumentation}
102 information, such as the {@link android.content.Context} for the app under
103 test. Instrumented tests can be used for unit, user interface (UI), or app
104 component integration testing. To learn how to develop instrumented tests for
105 your specific needs, see these additional topics:
106</p>
Quddus Chonge3f6c812015-08-26 14:16:05 -0700107
Quddus Chonge3f6c812015-08-26 14:16:05 -0700108<ul>
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700109 <li>
110 <a href=
111 "{@docRoot}training/testing/unit-testing/instrumented-unit-tests.html">Building
112 Instrumented Unit Tests</a> - Build more complex unit tests that have
113 Android dependencies which cannot be easily filled by using mock objects.
114 </li>
115
116 <li>
117 <a href="{@docRoot}training/testing/ui-testing/index.html">Automating User
118 Interface Tests</a> - Create tests to verify that the user interface
119 behaves correctly for user interactions within a single app or for
120 interactions across multiple apps.
121 </li>
122
123 <li>
124 <a href="{@docRoot}training/testing/integration-testing/index.html">Testing
125 App Component Integrations</a> - Verify the behavior of components that
126 users do not directly interact with, such as a <a href=
127 "{@docRoot}guide/components/services.html">Service</a> or a <a href=
128 "guide/topics/providers/content-providers.html">Content Provider</a>.
129 </li>
Quddus Chonge3f6c812015-08-26 14:16:05 -0700130</ul>
131
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700132<p>
133 This lesson teaches you how to build and run your tests using using Android
134 Studio. If you are not using Android Studio, you can learn how to
135 <a href="{@docRoot}tools/testing/testing_otheride.html">run your tests from
136 the command-line</a>.
137</p>
Quddus Chonge3f6c812015-08-26 14:16:05 -0700138
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700139<h3 id="config-local-tests">
140 Configure Your Project for Local Unit Tests
141</h3>
142
143<p>
144 In your Android Studio project, you must store the source files for local
145 unit tests under a specific source directory ({@code src/test/java}). This
146 improves project organization by grouping your unit tests together into a
147 single source set.
148</p>
149
150<p>
151 As with production code, you can create local unit tests for a <a href=
152 "{@docRoot}tools/building/configuring-gradle.html#workBuildVariants">specific
153 flavor or build type</a>. Keep your unit tests in a test source tree location
154 that corresponds to your production source tree, such as:
155</p>
Quddus Chonge3f6c812015-08-26 14:16:05 -0700156
157<table>
158<tr>
159<th>Path to Production Class</th>
160<th>Path to Local Unit Test Class</th>
161</tr>
162<tr>
163<td>{@code src/main/java/Foo.java}</td>
164<td>{@code src/test/java/FooTest.java}</td>
165</tr>
166<tr>
167<td>{@code src/debug/java/Foo.java}</td>
168<td>{@code src/testDebug/java/FooTest.java}</td>
169</tr>
170<tr>
171<td>{@code src/myFlavor/java/Foo.java}</td>
172<td>{@code src/testMyFlavor/java/FooTest.java}</td>
173</tr>
174</table>
175
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700176<p>
177 You'll need to configure the testing dependencies for your project to use the
178 standard APIs provided by the JUnit 4 framework. If your test needs to
179 interact with Android dependencies, include the <a href=
180 "https://github.com/mockito/mockito" class="external-link">Mockito</a>
181 library to simplify your local unit tests. To learn more about using mock
182 objects in your local unit tests, see <a href=
183 "{@docRoot}training/testing/unit-testing/local-unit-tests.html#mocking-dependencies">
184 Mocking Android dependencies</a>.
185</p>
186
187<p>
188 In your app's top-level {@code build.gradle} file, you need to specify these
189 libraries as dependencies:
190</p>
Quddus Chonge3f6c812015-08-26 14:16:05 -0700191
192<pre>
193dependencies {
194 // Required -- JUnit 4 framework
195 testCompile 'junit:junit:4.12'
196 // Optional -- Mockito framework
197 testCompile 'org.mockito:mockito-core:1.10.19'
198}
199</pre>
200
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700201<h3 id="config-instrumented-tests">
202 Configure Your Project for Instrumented Tests
203</h3>
204
Quddus Chonge3f6c812015-08-26 14:16:05 -0700205<p>
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700206 In your Android Studio project, you must place the source code for your
207 instrumentated tests under a specific directory
208 (<code>src/androidTest/java</code>).
Quddus Chonge3f6c812015-08-26 14:16:05 -0700209</p>
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700210
Quddus Chonge3f6c812015-08-26 14:16:05 -0700211<p>
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700212 <a href="{@docRoot}tools/testing-support-library/index.html#setup">Download
213 the Android Testing Support Library Setup</a>, which provides APIs that allow
214 you to quickly build and run instrumented test code for your apps. The
215 Testing Support Library includes a JUnit 4 test runner (<a href=
216 "{@docRoot}tools/testing-support-library/index.html#AndroidJUnitRunner">AndroidJUnitRunner</a>
217 ) and APIs for functional UI tests (<a href=
218 "{@docRoot}tools/testing-support-library/index.html#Espresso">Espresso</a>
219 and <a href=
220 "{@docRoot}tools/testing-support-library/index.html#UIAutomator">UI
221 Automator</a>).
Quddus Chonge3f6c812015-08-26 14:16:05 -0700222</p>
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700223
224<p>
225 You'll need to configure the Android testing dependencies for your project to
226 use the test runner and the rules APIs provided by the Testing Support
227 Library. To simplify your test development, we also recommend that you
228 include the <a href="https://github.com/hamcrest" class=
229 "external-link">Hamcrest</a> library, which lets you create more flexible
230 assertions using the Hamcrest matcher APIs.
231</p>
232
233<p>
234 In your app's top-level {@code build.gradle} file, you need to specify these
235 libraries as dependencies:
236</p>
237
Quddus Chonge3f6c812015-08-26 14:16:05 -0700238<pre>
239dependencies {
240 androidTestCompile 'com.android.support:support-annotations:23.0.1'
241 androidTestCompile 'com.android.support.test:runner:0.4.1'
242 androidTestCompile 'com.android.support.test:rules:0.4.1'
243 // Optional -- Hamcrest library
244 androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
245 // Optional -- UI testing with Espresso
246 androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
247 // Optional -- UI testing with UI Automator
248 androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
249}
250</pre>
251
Quddus Chonge3f6c812015-08-26 14:16:05 -0700252<p>
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700253 To use JUnit 4 test classes, make sure to specify <a href=
254 "{@docRoot}reference/android/support/test/runner/AndroidJUnitRunner.html">{@code
255 AndroidJUnitRunner}</a> as the default test instrumentation runner in your
256 project by including the following setting in your app's module-level {@code build.gradle}
257 file:
Quddus Chonge3f6c812015-08-26 14:16:05 -0700258</p>
Quddus Chonge3f6c812015-08-26 14:16:05 -0700259
260<pre>
261android {
262 defaultConfig {
263 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
264 }
265}
266</pre>
267
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700268<h2 id="build">
269 Build and Run Your Tests
270</h2>
271
272<p>
273 Android Studio provides all the tools you need to build, run, and analyze
274 your tests within the development environment. You can also run instrumented
275 tests on multiple device configurations, simultaneously, using <a href=
276 "https://developers.google.com/cloud-test-lab/">Cloud Test Lab</a>
277 integration.
278</p>
279
280<p class="note">
281 <strong>Note:</strong> While running or debugging instrumented tests,
282 Android Studio does not inject the additional methods required for <a href=
283 "{@docRoot}tools/building/building-studio.html#instant-run">Instant Run</a>
284 and turns the feature off.
285</p>
286
287<h3 id="run-local-tests">
288 Run Local Unit Tests
289</h3>
290
291<p>
292 To run your local unit tests:
293</p>
294
Quddus Chonge3f6c812015-08-26 14:16:05 -0700295<ol>
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700296 <li>In the <em>Project</em> window, right click on the project and
297 synchronize your project.
298 </li>
299
300 <li>Open the <em>Build Variants</em> window by clicking the left-hand tab,
301 then change the test artifact to <em>Unit Tests</em>.
302 </li>
303
304 <li>In the <em>Project</em> window, navigate to your unit test class or
305 method, then right-click it and select <strong>Run</strong> <img src=
306 "{@docRoot}images/tools/as-run.png" alt="" style=
307 "vertical-align:bottom;margin:0;">.
308 <ul>
309 <li>To run all tests in the unit test directory, right-click on the
310 directory and select <strong>Run tests</strong> <img src=
311 "{@docRoot}images/tools/as-run.png" alt="" style=
312 "vertical-align:bottom;margin:0;">.
313 </li>
314 </ul>
315 </li>
Quddus Chonge3f6c812015-08-26 14:16:05 -0700316</ol>
317
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700318<p>
319 The Android Plugin for Gradle compiles the local unit test code located in
320 the default directory ({@code src/test/java}), builds a test app, and
321 executes it locally using the default test runner class. Android Studio then
322 displays the results in the <em>Run</em> window.
323</p>
Quddus Chonge3f6c812015-08-26 14:16:05 -0700324
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700325<h3 id="run-instrumented-tests">
326 Run Instrumented Tests
327</h3>
Quddus Chonge3f6c812015-08-26 14:16:05 -0700328
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700329<p>
330 To run your instrumented tests:
331</p>
Quddus Chonge3f6c812015-08-26 14:16:05 -0700332
Adarsh Fernando44eb11a2016-03-29 12:56:50 -0700333<ol>
334 <li>Open the <em>Build Variants</em> window by clicking the left-hand tab,
335 then set the test artifact to <em>Android Instrumentation Tests</em>.
336 </li>
337
338 <li>In the <em>Project</em> window, navigate to your instrumented test class
339 or method, then right-click and run it using the Android Test configuration.
340 To run all tests in the instrumented test directory, right-click the
341 directory and select <strong>Run tests</strong> <img src=
342 "{@docRoot}images/tools/as-run.png" alt="" style=
343 "vertical-align:bottom;margin:0;">.
344 </li>
345</ol>
346
347<p>
348 The <a href="{@docRoot}tools/building/plugin-for-gradle.html">Android Plugin
349 for Gradle</a> compiles the instrumented test code located in the default
350 directory ({@code src/androidTest/java}), builds a test APK and production
351 APK, installs both APKs on the connected device or emulator, and runs the
352 tests. Android Studio then displays the results of the instrumented test execution in the
353 <em>Run</em> window.
354</p>
355
356<h3 id="run-ctl">Run Instrumented Tests with Cloud Test Lab</h3>
357
358<p>
359 Using <a href="https://developers.google.com/cloud-test-lab/">Cloud Test
360 Lab</a>, you can simultaneously test your app on many popular Android
361 devices, across multiple languages, screen orientations, and versions of the
362 Android platform. These tests run on actual physical devices in remote Google
363 data centers. You can also <a href=
364 "https://developers.google.com/cloud-test-lab/test-screenshots">configure
365 your instrumented tests to take screenshots</a> while Cloud Test Lab runs its
366 tests. You can <a href=
367 "https://developers.google.com/cloud-test-lab/command-line">deploy tests to
368 Cloud Test Lab from the command line</a>, or from Android Studio's integrated
369 testing tools.
370</p>
371
372<p>
373 Android Studio allows you to connect to your Google Cloud Platform account,
374 configure your tests, deploy them to Cloud Test Lab, and analyze the results
375 all within the development environment. Cloud Test Lab in Android Studio
376 supports the following Android test frameworks: <a href=
377 "{@docRoot}training/testing/ui-testing/espresso-testing.html">Espresso</a>,
378 <a href="{@docRoot}tools/testing-support-library/index.html#UIAutomator">UI
379 Automator 2.0</a>, or <a class="external-link" href=
380 "https://github.com/robotiumtech/robotium">Robotium</a>. Test results provide
381 test logs and include the details of any app failures.
382</p>
383
384<p>
385 Before you can start using Cloud Test Lab, you need to:
386</p>
387
388<ol>
389 <li>
390 <a href="https://console.developers.google.com/freetrial">Create a
391 Google Cloud Platform account</a> to use with active billing.
392 </li>
393
394 <li>
395 <a href="https://support.google.com/cloud/answer/6251787">Create a Google
396 Cloud project</a> for your app.
397 </li>
398
399 <li>
400 <a href="https://support.google.com/cloud/answer/6288653">Set up an active
401 billing account</a> and associate it with the project you just created.
402 </li>
403</ol>
404
405<h4 id="configure-matrix">
406Configure a test matrix and run a test
407</h4>
408
409<p>
410 Android Studio provides integrated tools that allow you to configure how you
411 want to deploy your tests to Cloud Test Lab. After you have created a Google
412 Cloud project with active billing, you can create a test configuration and
413 run your tests:
414</p>
415
416<ol>
417 <li>Click <strong>Run</strong> &gt; <strong>Edit Configurations</strong> from
418 the main menu.
419 </li>
420
421 <li>Click <strong>Add New Configuration (+)</strong> and select
422 <strong>Android Tests</strong>.
423 </li>
424
425 <li>In the Android Test configuration dialog:
426 <ol type="a">
427 <li>Enter or select the details of your test, such as the test name, module
428 type, test type, and test class.
429 </li>
430
431 <li>From the <em>Target</em> drop-down menu under <em>Deployment Target
432 Options</em>, select <strong>Cloud Test Lab Device Matrix</strong>.
433 </li>
434
435 <li>If you are not logged in, click <strong>Connect to Google Cloud
436 Platform</strong> and allow Android Studio access to your account.
437 </li>
438
439 <li>Next to <em>Cloud Project</em>, click the <img src=
440 "{@docRoot}images/tools/as-wrench.png" alt="wrench and nut" style=
441 "vertical-align:bottom;margin:0;"> button and select your Google Cloud
442 Platform project from the list.
443 </li>
444 </ol>
445 </li>
446
447 <li>Create and configure a test matrix:
448 <ol type="a">
449 <li>Next to the <em>Matrix Configuration</em> drop-down list, click <strong>
450 Open Dialog</strong> <img src="{@docRoot}images/tools/as-launchavdm.png"
451 alt="ellipses button" style="vertical-align:bottom;margin:0;">.
452 </li>
453
454 <li>Click <strong>Add New Configuration (+)</strong>.
455 </li>
456
457 <li>In the <strong>Name</strong> field, enter a name for your new
458 configuration.
459 </li>
460
461 <li>Select the device(s), Android version(s), locale(s) and screen
462 orientation(s) that you want to test your app with. Cloud Test Lab will test
463 your app against every combination of your selections when generating test
464 results.
465 </li>
466
467 <li>Click <strong>OK</strong> to save your configuration.
468 </li>
469 </ol>
470 </li>
471
472 <li>Click <strong>OK</strong> in the <em>Run/Debug Configurations</em> dialog
473 to exit.
474 </li>
475
476 <li>Run your tests by clicking <strong>Run</strong> <img src=
477 "{@docRoot}images/tools/as-run.png" alt="" style=
478 "vertical-align:bottom;margin:0;">.
479 </li>
480</ol>
481
482<h4 id="ctl-results">
483 Analyzing test results
484</h4>
485
486<p>
487 When Cloud Test Lab completes running your tests, the <em>Run</em> window will
488 open to show the results, as shown in figure 1. You may need to click
489 <strong>Show Passed</strong> <img src="{@docRoot}images/tools/as-ok.png" alt=
490 "" style="vertical-align:bottom;margin:0;"> to see all your executed tests.
491</p>
492
493<p>
494 <img src="{@docRoot}images/training/ctl-test-results.png" alt="">
495</p>
496
497<p class="img-caption">
498 <strong>Figure 1.</strong> Viewing the results of instrumented tests using
499 Cloud Test Lab.
500</p>
501
502<p>
503 You can also analyze your tests on the web by following the link displayed at
504 the beginning of the test execution log in the <em>Run</em> window, as shown
505 in figure 2.
506</p>
507
508<p>
509 <img src="{@docRoot}images/training/ctl-exec-log.png" alt="">
510</p>
511
512<p class="img-caption">
513 <strong>Figure 2.</strong> Click the link to view detailed test results on
514 the web.
515</p>
516
517<p>
518 To learn more about interpreting web results, see <a href=
519 "https://developers.google.com/cloud-test-lab/analyzing-results">Analyzing
520 Cloud Test Lab Web Results</a>.
521</p>