diff --git a/docs/html/images/testing/test-types.zip b/docs/html/images/testing/test-types.zip new file mode 100644 index 0000000000000..d433b915ff15a Binary files /dev/null and b/docs/html/images/testing/test-types.zip differ diff --git a/docs/html/images/testing/test-types_2x.png b/docs/html/images/testing/test-types_2x.png new file mode 100644 index 0000000000000..0a374aaf65382 Binary files /dev/null and b/docs/html/images/testing/test-types_2x.png differ diff --git a/docs/html/training/testing/start/index.jd b/docs/html/training/testing/start/index.jd index 707ba9d8f65a1..aa0473f6d09f1 100644 --- a/docs/html/training/testing/start/index.jd +++ b/docs/html/training/testing/start/index.jd @@ -9,51 +9,20 @@ page.image=images/tools/studio-main-screen.png
- 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. + Android tests are based on JUnit, and you can run them either as local + unit tests on the JVM or as instrumented tests on an Android device. + This page provides an introduction to the concepts and + tools for building Android 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. -
-- 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: -
+When using Android Studio to write any of your tests, your test code must go +into one of two different code directories (source sets). For each module in +your project, Android Studio includes both source sets, corresponding to the +following test types:
+ +module-name/src/test/java/.
+These tests run on the local JVM +and do not have access to functional Android framework APIs.
+To get started, see Building Local +Unit Tests.
+module-name/src/androidTest/java/.
+These are all tests that must run on an Android hardware device or +an Android emulator.
+ +Instrumented tests are built into an APK that runs on the device alongside +your app under test. The system runs your test APK and your app under tests in +the same process, so your tests can invoke methods and modify fields in the +app, and automate user interaction with your app.
+ +For information about how to create instrumented tests, see the +following topics:
- 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. -
+ + -
-- 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: -
+However, the local unit tests and instrumented tests +described above are just terms that help distinguish the tests that run on your +local JVM from the tests that run on the Android platform (on a hardware device +or emulator). The real testing types that you should understand when building a +complete test suite are described in the following table.
| Path to Production Class | -Path to Local Unit Test Class | -|
|---|---|---|
| {@code src/main/java/Foo.java} | -{@code src/test/java/FooTest.java} | -|
| {@code src/debug/java/Foo.java} | -{@code src/testDebug/java/FooTest.java} | -|
| {@code src/myFlavor/java/Foo.java} | -{@code src/testMyFlavor/java/FooTest.java} | -|
| + Type + | ++ Subtype + | ++ Description + | +
| + Unit tests + | +||
| + Local Unit Tests + | ++ Unit tests that run locally on the Java Virtual Machine (JVM). Use these +tests to minimize execution time when your tests have no Android framework +dependencies or when you can mock the Android framework dependencies. + | +|
| + Instrumented unit tests + | +
+ Unit tests that run on an Android device or emulator. These tests have
+access to Instrumentation
+information, such as the Context of the app you are
+testing. Use these tests when your tests have Android dependencies that mock
+objects cannot satisfy.
+ |
+ |
| + Integration Tests + | +||
| + Components within your app only + | ++ This type of test verifies that the target app behaves as expected when +a user performs a specific action or enters a specific input in its activities. +For example, it allows you to check that the target app returns the correct UI +output in response to user interactions in the app’s activities. UI testing +frameworks like Espresso allow +you to programmatically simulate user actions and test complex intra-app user +interactions. + | +|
| + Cross-app Components + | ++ This type of test verifies the correct behavior of interactions between +different user apps or between user apps and system apps. For example, you might +want to test that your app behaves correctly when the user performs an action +in the Android Settings menu. UI testing frameworks that support cross-app +interactions, such as UI Automator, allow you to create tests for such scenarios. + | +
- 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: -
+ + + + +The following are common APIs used for testing apps on Android.
+ + +You should write your unit or integration test class as a JUnit 4 test class. The framework +offers a convenient way to perform common setup, teardown, and assertion +operations in your test.
+ +A basic JUnit 4 test class is a Java class that contains one or more test
+methods. A test method begins with the @Test annotation and
+contains the code to exercise and verify a single functionality (that is, a
+logical unit) in the component that you want to test.
The following snippet shows an example JUnit 4 integration test that uses the +Espresso +APIs to perform a click action on a UI element, then checks to see if +an expected string is displayed.
-dependencies {
- // Required -- JUnit 4 framework
- testCompile 'junit:junit:4.12'
- // Optional -- Mockito framework
- testCompile 'org.mockito:mockito-core:1.10.19'
-}
-
+@RunWith(AndroidJUnit4.class)
+@LargeTest
+public class MainActivityInstrumentationTest {
-
- 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 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 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'
- androidTestCompile 'com.android.support.test:runner:0.4.1'
- androidTestCompile 'com.android.support.test:rules:0.4.1'
- // Optional -- Hamcrest library
- androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
- // Optional -- UI testing with Espresso
- androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
- // Optional -- UI testing with UI Automator
- androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
-}
-
-
-
-- 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: -
- -
-android {
- defaultConfig {
- testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
+ onView(withId(R.id.textView)).check(matches(withText("Hello, World!")));
}
}
-Android Studio has two types of test artifacts: Android Instrumentation Tests -and Unit Tests. Previously, you could work with just one test artifact at a -time. Now, both test artifacts are enabled. -The advantage of enabling both test artifacts is that any changes you make to -the underlying code affect -them both. For example, if you rename a class that both test artifacts access, -both will know about the class name refactoring.
- -The figure shows what your project looks like with both test -artifacts enabled. Notice the shading of both test artifacts.
- - -
-
-
-- 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. -
- -- To run your 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. Android Studio then - displays the results in the Run window. -
- -- To run your instrumented tests: -
+In your JUnit 4 test class, you can call out sections in your test code for +special processing by using the following annotations:
@Before: Use this annotation to specify a block of code that
+contains test setup operations. The test class invokes this code block before
+each test. You can have multiple @Before methods but the order in
+which the test class calls these methods is not guaranteed.@After: This annotation specifies a block of code that
+contains test tear-down operations. The test class calls this code block after
+every test method. You can define multiple @After operations in
+your test code. Use this annotation to release any resources from memory.@Test: Use this annotation to mark a test method. A single
+test class can contain multiple test methods, each prefixed with this
+annotation.@Rule: Rules allow you to flexibly add or redefine the
+behavior of each test method in a reusable way. In Android testing, use this
+annotation together with one of the test rule classes that the Android Testing
+Support Library provides, such as ActivityTestRule
+or ServiceTestRule.@BeforeClass: Use this annotation to specify static methods
+for each test class to invoke only once. This testing step is useful for
+expensive operations such as connecting to a database.@AfterClass: Use this annotation to specify static methods for
+the test class to invoke only after all tests in the class have run. This
+testing step is useful for releasing any resources allocated in the
+@BeforeClass block.@Test(timeout=): Some annotations support the ability to pass
+in elements for which you can set values. For example, you can specify a
+timeout period for the test. If the test starts but does not complete within
+the given timeout period, it automatically fails. You must specify the timeout
+period in milliseconds, for example: @Test(timeout=5000).- 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. +
For more annotations, see the documentation for JUnit annotations and the Android +annotations.
+ +Use the JUnit Assert class to verify the
+correctness of an object's state. The assert methods compare values you expect
+from a test to the actual results and throw an exception if the comparison
+fails. Assertion classes describes these
+methods in more detail.
The Android +Testing Support Library provides a set of APIs that allow you +to quickly build and run test code for your apps, including JUnit 4 and +functional UI tests. The library includes the following instrumentation-based +APIs that are useful when you want to automate your tests:
+ +Because Android Testing Support Library APIs extend JUnit, you can use +assertion methods to display the results of tests. An assertion method compares +an actual value returned by a test to an expected value, and throws an +AssertionException if the comparison test fails. Using assertions is more +convenient than logging, and provides better test performance.
+ +To simplify test development, you should use the Hamcrest library, which +lets you create more flexible tests using the Hamcrest matcher APIs.
+ + + +The Android SDK includes two tools for functional-level app testing:
+ +monkeyrunner command-line tool.The following documents provide more detail about how to build and run +a variety of test types:
-- 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: -
- -- 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: -
- -
.
-
-- Figure 1. Creating a test configuration for Cloud Test - Lab. -
- -
- When Cloud Test Lab completes running your tests, the Run window will
- open to show the results, as shown in figure 2. You may need to click
- Show Passed
to see all your executed tests.
-
-
-- Figure 2. 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 3. -
- -
-
-- Figure 3. 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 +-Instrumented unit tests are unit tests that run on physical devices and emulators, instead of -the Java Virtual Machine (JVM) on your local machine. You should create instrumented unit tests -if your tests need access to instrumentation information (such as the target app's -{@link android.content.Context}) or if they require the real implementation of an Android framework -component (such as a {@link android.os.Parcelable} or {@link android.content.SharedPreferences} -object). Using instrumented unit tests also helps to reduce the effort required to write and -maintain mock code. You are still free to use a mocking framework, if you choose, to simulate any -dependency relationships. Instrumented unit tests can take advantage of the Android framework APIs -and supporting APIs, such as the Android Testing Support Library. -
+Instrumented unit tests are tests that run on physical devices and +emulators, and they can take advantage of the Android framework APIs and +supporting APIs, such as the Android Testing Support Library. You should create +instrumented unit tests if your tests need access to instrumentation +information (such as the target app's {@link android.content.Context}) or if +they require the real implementation of an Android framework component (such as +a {@link android.os.Parcelable} or {@link android.content.SharedPreferences} +object).
+ +Using instrumented unit tests also helps to reduce the effort required to +write and maintain mock code. You are still free to use a mocking framework, if +you choose, to simulate any dependency relationships.
+Before building your instrumented unit test, make sure to configure your test source code -location and project dependencies, as described in - -Getting Started with Testing.
+ +In your Android Studio project, you must store the source files for
+instrumented tests at
+module-name/src/androidTests/java/. This directory
+already exists when you create a new project.
Before you begin, you should + 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 also 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, you should also 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:24.0.0'
+ androidTestCompile 'com.android.support.test:runner:0.5'
+ androidTestCompile 'com.android.support.test:rules:0.5'
+ // Optional -- Hamcrest library
+ androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
+ // Optional -- UI testing with Espresso
+ androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
+ // Optional -- UI testing with UI Automator
+ androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
+}
+
+
+
++ 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: +
+ +
+android {
+ defaultConfig {
+ testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
+ }
+}
+
+
+
+
Your instrumented unit test class should be written as a JUnit 4 test class. To learn more about creating JUnit 4 test classes and using JUnit 4 assertions and annotations, see @@ -119,7 +181,7 @@ public class LogHistoryAndroidUnitTest { } -
To organize the execution of your instrumented unit tests, you can group a collection of test classes in a test suite class and run these tests together. Test suites can be nested; @@ -162,9 +224,198 @@ import org.junit.runners.Suite; public class UnitTestSuite {} +
To run your instrumented tests, follow these steps:
+ +
in the toolbar.-To run your test, follow the steps for running instrumented tests -described in -Getting Started with Testing. + 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. +
+ ++ Note: While running or debugging instrumented tests, + Android Studio does not inject the additional methods required for Instant Run + and turns the feature off. +
+ + +Using Firebase Test +Lab, you can simultaneously test your app on many popular Android devices +and configurations such as locale, orientation, screen size, and platform +version. These tests run on actual physical devices in remote Google data +centers. You can deploy to Firebase Test Lab directly from Android Studio or +from the command line. Test results provide test logs and include the details +of any app failures.
+ ++ Before you can start using Firebase Test Lab, you need to: +
+ ++ Android Studio provides integrated tools that allow you to configure how you + want to deploy your tests to Firebase Test Lab. After you have created a Google + Cloud project with active billing, you can create a test configuration and + run your tests: +
+ +
.
+
++ Figure 1. Creating a test configuration for Firebase Test + Lab. +
+ +
+ When Firebase Test Lab completes running your tests, the Run window
+ will open to show the results, as shown in figure 2. You may need to click
+ Show Passed
to see all your executed tests.
+
+
++ Figure 2. Viewing the results of instrumented tests using + Firebase 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 3. +
+ +
+
++ Figure 3. Click the link to view detailed test results on + the web. +
+ ++ To learn more about interpreting web results, see Analyze + Firebase Test Lab for Android Results.
\ No newline at end of file diff --git a/docs/html/training/testing/unit-testing/local-unit-tests.jd b/docs/html/training/testing/unit-testing/local-unit-tests.jd index 893d957fcc41c..25b62fa2e3932 100644 --- a/docs/html/training/testing/unit-testing/local-unit-tests.jd +++ b/docs/html/training/testing/unit-testing/local-unit-tests.jd @@ -7,17 +7,16 @@ trainingnavtop=trueBefore building your local unit test, make sure to configure your test source code location and -project dependencies, as described in - -Getting Started with Testing.
+ +In your Android Studio project, you must store the source files for local
+unit tests at module-name/src/test/java/. This directory
+already exists when you create a new project.
You also 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 {
+ // Required -- JUnit 4 framework
+ testCompile 'junit:junit:4.12'
+ // Optional -- Mockito framework
+ testCompile 'org.mockito:mockito-core:1.10.19'
+}
+
Your local unit test class should be written as a JUnit 4 test class. JUnit is the most popular and widely-used unit testing framework for Java. The latest version of this framework, JUnit 4, @@ -90,7 +111,7 @@ can use Hamcrest matchers (such as the {@code is()} and {@code equalTo()} methods) to match the returned result against the expected result.
-By default, the Android Plug-in for Gradle executes your local unit tests against a modified @@ -174,10 +195,37 @@ class="external-link">Mockito API reference and the class="external-link">sample code.
--To run your tests, follow the steps for running local unit tests -described in -Getting Started with Testing. -
+To run your local unit tests, follow these steps:
+ +
in the toolbar.+ 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. +