diff --git a/docs/html/images/tools/as-launchavdm.png b/docs/html/images/tools/as-launchavdm.png index bf159810adf52..f2c7bf35adad3 100644 Binary files a/docs/html/images/tools/as-launchavdm.png and b/docs/html/images/tools/as-launchavdm.png differ diff --git a/docs/html/images/tools/as-ok.png b/docs/html/images/tools/as-ok.png new file mode 100644 index 0000000000000..244a77fe707ca Binary files /dev/null and b/docs/html/images/tools/as-ok.png differ diff --git a/docs/html/images/tools/as-wrench.png b/docs/html/images/tools/as-wrench.png new file mode 100644 index 0000000000000..5886487f233d5 Binary files /dev/null and b/docs/html/images/tools/as-wrench.png differ diff --git a/docs/html/images/training/ctl-exec-log.png b/docs/html/images/training/ctl-exec-log.png new file mode 100644 index 0000000000000..96bbb81160f1f Binary files /dev/null and b/docs/html/images/training/ctl-exec-log.png differ diff --git a/docs/html/images/training/ctl-test-results.png b/docs/html/images/training/ctl-test-results.png new file mode 100644 index 0000000000000..bb2fc93d542c1 Binary files /dev/null and b/docs/html/images/training/ctl-test-results.png differ diff --git a/docs/html/training/testing/start/index.jd b/docs/html/training/testing/start/index.jd index a4b4aea9ef017..74617b0cc64c7 100644 --- a/docs/html/training/testing/start/index.jd +++ b/docs/html/training/testing/start/index.jd @@ -6,69 +6,153 @@ page.image=images/tools/studio-main-screen.png @jd:body
-
+
- -

Dependencies and prerequisites

- +

+ Dependencies and prerequisites +

-

This lesson teaches you to

-
    -
  1. Set Up Your Testing Environment
  2. -
  3. Build and Run Your Tests
  4. -
+ -
+

+ This lesson teaches you to +

+ +
    +
  1. + Configure Your Project for Local Unit + Tests +
  2. + +
  3. + Configure Your Project for + Instrumented Tests +
  4. + +
  5. + Build and Run Your Tests +
      +
    1. + Run Local Unit Tests +
    2. + +
    3. + Run Instrumented Tests +
    4. + +
    5. + Run Instrumented Tests with Cloud Test Lab +
    6. +
    +
  6. +
+ +

+ See also +

+ + +
-

You should be writing and running tests as part of your Android app development cycle. -Well-written tests can help you catch bugs early in development and give you confidence in your -code.

+

+ Writing and running tests are important parts of the Android app development + cycle. Well-written tests can help you catch bugs early in development and + give you confidence in your code. Using Android Studio, you can run local + unit tests or instrumented tests on a variety of physical or virtual Android + devices. You can then analyze the results and make changes to your code + without leaving the development environment. +

-

To verify specific behavior in your app, and to check for consistency across different Android -devices, you can write a test case. This lesson teaches you how to build a test case using the -JUnit 4 framework and the testing APIs and tools provided by Google, and how to run your -tests.

+

+ Local unit tests are tests that run on your local machine, without + needing access to the Android framework or an Android device. To learn how to + develop local units tests, see Building + Local Unit Tests. +

-

Set Up Your Testing Environment

+

+ Instrumented tests are tests that run on an Android device or + emulator. These tests have access to {@link android.app.Instrumentation} + information, such as the {@link android.content.Context} for the app under + test. Instrumented tests can be used for unit, user interface (UI), or app + component integration testing. To learn how to develop instrumented tests for + your specific needs, see these additional topics: +

-

Before you start writing and running your tests, you must set up your test -development environment. Android Studio provides an integrated development environment for you to -create, build, and run Android app test cases from a graphical user interface (GUI).

- -

You must first download the prerequisite Android development tools before proceeding:

-

Based on the type of test you want to create, configure the test code source location and the - project dependencies in Android Studio as described in the following sections.

+

+ This lesson teaches you how to build and run your tests using using Android + Studio. If you are not using Android Studio, you can learn how to + run your tests from + the command-line. +

-

Configure Your Project for Local Unit Tests

-

Local unit tests are tests that run on your local machine, without needing access to the -Android framework or an Android device. To learn how to develop local units tests, see - -Building Local Unit Tests.

-

In your Android Studio project, you must store the source files for local unit tests under a -specific source directory ({@code src/test/java}). This feature improves your project organization -by letting you group your unit tests together into a single source set.

-

As with production code, you can create local unit tests for a -specific flavor or build type. Keep your unit tests in a test -source tree location that corresponds to your production source tree, such as:

+

+ Configure Your Project for Local Unit Tests +

+ +

+ In your Android Studio project, you must store the source files for local + unit tests under a specific source directory ({@code src/test/java}). This + improves project organization by grouping your unit tests together into a + single source set. +

+ +

+ As with production code, you can create local unit tests for a specific + flavor or build type. Keep your unit tests in a test source tree location + that corresponds to your production source tree, such as: +

@@ -89,15 +173,21 @@ source tree location that corresponds to your production source tree, such as:
-

You'll need to configure the testing dependencies for your project to use the - standard APIs provided by the JUnit 4 framework. To simplify your local unit test development, - we recommend that you include the Mockito library if your test needs to interact with Android - dependencies. To learn more about using mock objects in your local unit tests, see - - Mocking Android dependencies.

-

In the {@code build.gradle} file of your Android app module, specify your dependencies like -this:

+

+ You'll need to configure the testing dependencies for your project to use the + standard APIs provided by the JUnit 4 framework. If your test needs to + interact with Android dependencies, include the Mockito + library to simplify your local unit tests. To learn more about using mock + objects in your local unit tests, see + Mocking Android dependencies. +

+ +

+ In your app's top-level {@code build.gradle} file, you need to specify these + libraries as dependencies: +

 dependencies {
@@ -108,46 +198,43 @@ dependencies {
 }
 
-

Configure Your Project for Instrumented Tests

-

Instrumented tests are tests that run on an Android device or emulator. These tests -have access to {@link android.app.Instrumentation} information, such as the -{@link android.content.Context} for the app under test. Instrumented tests can be used for unit, -user interface (UI), or app component integration testing. To learn how to develop instrumented -tests for your specific needs, see these additional topics: -

-

+

+ Configure Your Project for Instrumented Tests +

+

-In your Android Studio project, you must place the source code for your instrumentated tests under -a specific directory (src/androidTest/java). + In your Android Studio project, you must place the source code for your + instrumentated tests under a specific directory + (src/androidTest/java).

+

-Download the Android Testing Support Library, which provides APIs that allow you to quickly build and -run instrumented test code for your apps. The Testing Support Library includes a JUnit 4 test runner -(AndroidJUnitRunner -) and APIs for functional UI tests -(Espresso and -UI Automator). To -learn how to install the library, see -Testing Support Library Setup. + Download + the Android Testing Support Library Setup, which provides APIs that allow + you to quickly build and run instrumented test code for your apps. The + Testing Support Library includes a JUnit 4 test runner (AndroidJUnitRunner + ) and APIs for functional UI tests (Espresso + and UI + Automator).

-

You'll need to configure the Android testing dependencies for your project to use the test runner -and the rules APIs provided by the Testing Support Library. To simplify your test development, -we also recommend that you include the Hamcrest library, which lets you create more flexible assertions using the -Hamcrest matcher APIs.

-

In the {@code build.gradle} file of your Android app module, specify your dependencies like -this:

+ +

+ You'll need to configure the Android testing dependencies for your project to + use the test runner and the rules APIs provided by the Testing Support + Library. To simplify your test development, we also recommend that you + include the Hamcrest library, which lets you create more flexible + assertions using the Hamcrest matcher APIs. +

+ +

+ In your app's top-level {@code build.gradle} file, you need to specify these + libraries as dependencies: +

+
 dependencies {
     androidTestCompile 'com.android.support:support-annotations:23.0.1'
@@ -162,59 +249,13 @@ dependencies {
 }
 
-

Build and Run Your Tests

- -

You can run build and run your tests in a similar way to how you run your Android apps -- - graphically in Android Studio or from the command-line using the - -Android Plugin for Gradle.

- -

Run Local Unit Tests

-The Android Plugin for Gradle compiles the local unit test code located in the default directory -({@code src/test/java}), builds a test app, and executes it locally -using the default test runner class. + To use JUnit 4 test classes, make sure to specify {@code + AndroidJUnitRunner} as the default test instrumentation runner in your + project by including the following setting in your app's module-level {@code build.gradle} + file:

-

-To run local unit tests in your Gradle project from Android Studio: -

-
    -
  1. In the Project window, right click on the project and synchronize your project. -
  2. -
  3. Open the Build Variants window by clicking the left-hand tab, then change the -test artifact to Unit Tests. -
  4. -
  5. In the Project window, drill down to your unit test class or method, -then right-click and run it. To run all tests in the unit test directory, select the directory then -right-click and press Run tests. -
  6. -
- -

Android Studio displays the results of the unit test execution in the Run -window.

- -

To run local unit tests in your Gradle project from the command-line, call the {@code test} task -command.

- -
-./gradlew test
-
- -

If there are failing tests, the command will display links to HTML reports (one per build -variant). You can find the generated HTML test result reports in the -{@code <path_to_your_project>/app/build/reports/tests/} directory, and the corresponding XML -files in the {@code <path_to_your_project>/app/build/test-results/} directory.

- -

Run Instrumented Tests

-

-The Android Plugin for Gradle compiles the instrumented test code located in the default directory -({@code src/androidTest/java}), builds a test APK and production APK, installs both APKs on the -connected device or emulator, and executes the tests.

- -

Make sure to specify - -{@code AndroidJUnitRunner} as the default test instrumentation runner in your project. To do -this, add the following setting in your {@code build.gradle} file:

 android {
@@ -224,29 +265,257 @@ android {
 }
 
-

To run your instrumented tests in Android Studio:

+

+ Build and Run Your Tests +

+ +

+ Android Studio provides all the tools you need to build, run, and analyze + your tests within the development environment. You can also run instrumented + tests on multiple device configurations, simultaneously, using Cloud Test Lab + integration. +

+ +

+ Note: While running or debugging instrumented tests, + Android Studio does not inject the additional methods required for Instant Run + and turns the feature off. +

+ +

+ Run Local Unit Tests +

+ +

+ To run your local unit tests: +

+
    -
  1. Open the Build Variants window by clicking the left-hand tab, then set the -test artifact to Android Instrumentation Tests. -
  2. -
  3. In the Project window, drill down to your instrumented test class or method, - then right-click and run it using the Android Test configuration. To run all tests in the -instrumented test directory, select the directory then right-click and press -Run tests. -
  4. +
  5. In the Project window, right click on the project and + synchronize your project. +
  6. + +
  7. Open the Build Variants window by clicking the left-hand tab, + then change the test artifact to Unit Tests. +
  8. + +
  9. In the Project window, navigate to your unit test class or + method, then right-click it and select Run . + +
-

Android Studio displays the results of the instrumented test execution in the -Run window.

+

+ The Android Plugin for Gradle compiles the local unit test code located in + the default directory ({@code src/test/java}), builds a test app, and + executes it locally using the default test runner class. Android Studio then + displays the results in the Run window. +

-

To run your instrumented tests from the command-line via Gradle, call the - {@code connectedAndroidTest} (or {@code cAT}) task:

+

+ Run Instrumented Tests +

-
-./gradlew cAT
-
+

+ To run your instrumented tests: +

-

You can find the generated HTML test result reports in the -{@code <path_to_your_project>/app/build/outputs/reports/androidTests/connected/} directory, -and the corresponding XML files in the -{@code <path_to_your_project>/app/build/outputs/androidTest-results/connected/} directory.

\ No newline at end of file +
    +
  1. Open the Build Variants window by clicking the left-hand tab, + then set the test artifact to Android Instrumentation Tests. +
  2. + +
  3. In the Project window, navigate to your instrumented test class + or method, then right-click and run it using the Android Test configuration. + To run all tests in the instrumented test directory, right-click the + directory and select Run tests . +
  4. +
+ +

+ The Android Plugin + for Gradle compiles the instrumented test code located in the default + directory ({@code src/androidTest/java}), builds a test APK and production + APK, installs both APKs on the connected device or emulator, and runs the + tests. Android Studio then displays the results of the instrumented test execution in the + Run window. +

+ +

Run Instrumented Tests with Cloud Test Lab

+ +

+ Using Cloud Test + Lab, you can simultaneously test your app on many popular Android + devices, across multiple languages, screen orientations, and versions of the + Android platform. These tests run on actual physical devices in remote Google + data centers. You can also configure + your instrumented tests to take screenshots while Cloud Test Lab runs its + tests. You can deploy tests to + Cloud Test Lab from the command line, or from Android Studio's integrated + testing tools. +

+ +

+ Android Studio allows you to connect to your Google Cloud Platform account, + configure your tests, deploy them to Cloud Test Lab, and analyze the results + all within the development environment. Cloud Test Lab in Android Studio + supports the following Android test frameworks: Espresso, + UI + Automator 2.0, or Robotium. Test results provide + test logs and include the details of any app failures. +

+ +

+ Before you can start using Cloud Test Lab, you need to: +

+ +
    +
  1. + Create a + Google Cloud Platform account to use with active billing. +
  2. + +
  3. + Create a Google + Cloud project for your app. +
  4. + +
  5. + Set up an active + billing account and associate it with the project you just created. +
  6. +
+ +

+Configure a test matrix and run a test +

+ +

+ Android Studio provides integrated tools that allow you to configure how you + want to deploy your tests to Cloud Test Lab. After you have created a Google + Cloud project with active billing, you can create a test configuration and + run your tests: +

+ +
    +
  1. Click Run > Edit Configurations from + the main menu. +
  2. + +
  3. Click Add New Configuration (+) and select + Android Tests. +
  4. + +
  5. In the Android Test configuration dialog: +
      +
    1. Enter or select the details of your test, such as the test name, module + type, test type, and test class. +
    2. + +
    3. From the Target drop-down menu under Deployment Target + Options, select Cloud Test Lab Device Matrix. +
    4. + +
    5. If you are not logged in, click Connect to Google Cloud + Platform and allow Android Studio access to your account. +
    6. + +
    7. Next to Cloud Project, click the wrench and nut button and select your Google Cloud + Platform project from the list. +
    8. +
    +
  6. + +
  7. Create and configure a test matrix: +
      +
    1. Next to the Matrix Configuration drop-down list, click + Open Dialog ellipses button. +
    2. + +
    3. Click Add New Configuration (+). +
    4. + +
    5. In the Name field, enter a name for your new + configuration. +
    6. + +
    7. Select the device(s), Android version(s), locale(s) and screen + orientation(s) that you want to test your app with. Cloud Test Lab will test + your app against every combination of your selections when generating test + results. +
    8. + +
    9. Click OK to save your configuration. +
    10. +
    +
  8. + +
  9. Click OK in the Run/Debug Configurations dialog + to exit. +
  10. + +
  11. Run your tests by clicking Run . +
  12. +
+ +

+ Analyzing test results +

+ +

+ When Cloud Test Lab completes running your tests, the Run window will + open to show the results, as shown in figure 1. You may need to click + Show Passed + to see all your executed tests. +

+ +

+ +

+ +

+ Figure 1. Viewing the results of instrumented tests using + Cloud Test Lab. +

+ +

+ You can also analyze your tests on the web by following the link displayed at + the beginning of the test execution log in the Run window, as shown + in figure 2. +

+ +

+ +

+ +

+ Figure 2. Click the link to view detailed test results on + the web. +

+ +

+ To learn more about interpreting web results, see Analyzing + Cloud Test Lab Web Results. +

\ No newline at end of file