AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 1 | # Testing |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | AndroidX contains unit and integration tests that are run automatically when a |
| 6 | change is uploaded. It also contains a number of sample applications that are |
| 7 | useful for demonstrating how to use features as well as performing manual |
| 8 | testing. |
| 9 | |
| 10 | ## Adding tests {#adding} |
| 11 | |
| 12 | For an example of how to set up simple unit and integration tests in a new |
| 13 | module, see |
| 14 | [aosp/1189799](https://android-review.googlesource.com/c/platform/frameworks/support/+/1189799). |
| 15 | For an example of how to set up Espresso-powered integration tests, see the |
| 16 | `preference` library's |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 17 | [`build.gradle`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:preference/preference/build.gradle) |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 18 | and |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 19 | [`EditTextPreferenceTest.java`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:preference/preference/src/androidTest/java/androidx/preference/tests/EditTextPreferenceTest.java) |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 20 | files. |
| 21 | |
| 22 | The currently allowed test runners for on-device tests are |
| 23 | [`AndroidJUnitRunner`](https://developer.android.com/training/testing/junit-runner) |
| 24 | and |
| 25 | [`Parameterized`](https://junit.org/junit4/javadoc/4.12/org/junit/runners/Parameterized.html). |
| 26 | |
AndroidX Core Team | b5ba61d | 2021-06-08 09:20:36 -0700 | [diff] [blame] | 27 | NOTE All package/class/method combinations must be unique. Multiple copies of |
| 28 | the same class/method can be included e.g. under different directories, but must |
| 29 | be distinguishable by their packages. |
| 30 | |
AndroidX Core Team | 03b4da3 | 2021-03-10 23:20:41 +0000 | [diff] [blame] | 31 | NOTE For best practices on writing libraries in a way that makes it easy for end |
| 32 | users -- and library developers -- to write tests, see the |
| 33 | [Testability](testability.md) guide. |
| 34 | |
AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 35 | ### What gets tested, and when {#affected-module-detector} |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 36 | |
| 37 | We use the |
AndroidX Core Team | 4cc85fa | 2021-11-23 15:58:34 +0000 | [diff] [blame] | 38 | [AffectedModuleDetector](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:buildSrc/private/src/main/kotlin/androidx/build/dependencyTracker/AffectedModuleDetector.kt) |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 39 | to determine what projects have changed since the last merge. |
| 40 | |
| 41 | In presubmit, "affected" modules will run all host and device tests regardless |
alanv | 37fed3a2 | 2021-09-17 07:46:47 -0700 | [diff] [blame] | 42 | of size. Modules that *depend* on affected modules will run all host tests, but |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 43 | will only run device tests annotated with `@SmallTest` or `@MediumTest`. |
| 44 | |
| 45 | When changes are made that can't be associated with a module, are in the root of |
| 46 | the checkout, or are within `buildSrc`, then all host tests and all device tests |
| 47 | annotated with `@SmallTest` or `@MediumTest` will be run for all modules. |
| 48 | |
| 49 | Presubmit tests represent only a subset of the devices on which our tests run. |
| 50 | The remaining devices are tested only in postsubmit. In postsubmit, all host and |
| 51 | device tests are run for all modules. |
| 52 | |
AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 53 | ### Test annotations {#annotations} |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 54 | |
AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 55 | #### Test size and runners {#test-size} |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 56 | |
| 57 | All device tests *should* be given a size annotation, which is one of: |
| 58 | |
| 59 | * [`@SmallTest`](https://developer.android.com/reference/androidx/test/filters/SmallTest) |
| 60 | * [`@MediumTest`](https://developer.android.com/reference/androidx/test/filters/MediumTest) |
| 61 | * [`@LargeTest`](https://developer.android.com/reference/androidx/test/filters/LargeTest) |
| 62 | |
alanv | 37fed3a2 | 2021-09-17 07:46:47 -0700 | [diff] [blame] | 63 | If a device test is *not* annotated with its size, it will be run as if it were |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 64 | `@LargeTest` by default. Host tests do not need to be annotated with their size, |
| 65 | as all host tests are run regardless of size. |
| 66 | |
| 67 | This annotation can occur at either the class level or individual test level. |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 68 | |
AndroidX Core Team | b5ba61d | 2021-06-08 09:20:36 -0700 | [diff] [blame] | 69 | Annotation | Max duration |
| 70 | ------------- | ------------ |
| 71 | `@SmallTest` | 200ms |
| 72 | `@MediumTest` | 1000ms |
| 73 | `@LargeTest` | 100000ms |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 74 | |
AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 75 | #### Disabling tests {#disabling-tests} |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 76 | |
| 77 | To disable a device-side test in presubmit testing only -- but still have it run |
| 78 | in postsubmit -- use the |
| 79 | [`@FlakyTest`](https://developer.android.com/reference/androidx/test/filters/FlakyTest) |
| 80 | annotation. There is currently no support for presubmit-only disabling of |
| 81 | host-side tests. |
| 82 | |
| 83 | If you need to stop a host- or device-side test from running entirely, use |
| 84 | JUnit's [`@Ignore`](http://junit.sourceforge.net/javadoc/org/junit/Ignore.html) |
| 85 | annotation. Do *not* use Android's `@Suppress` annotation, which only works with |
| 86 | Android test runners and will *not* work for host-side tests. |
| 87 | |
AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 88 | #### Filtering devices {#filtering-devices} |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 89 | |
| 90 | To restrict a test to a range of SDKs, use |
| 91 | [`@SdkSuppress`](https://developer.android.com/reference/androidx/test/filters/SdkSuppress) |
| 92 | which allows specifying a range with `minSdkVersion` and `maxSdkVersion`. This |
| 93 | annotation also supports targeting a specific pre-release SDK with the |
| 94 | `codeName` parameter. |
| 95 | |
| 96 | ```java |
| 97 | // Target SDKs 17 through 19, inclusive |
| 98 | @SdkSuppress(minSdkVersion = 17, maxSdkVersion = 19) |
| 99 | |
AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 100 | // Target pre-release SDK T only |
| 101 | @SdkSuppress(minSdkVersion = Build.VERSION_CODES.TIRAMISU, codeName = "Tiramisu") |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 102 | ``` |
| 103 | |
| 104 | You may also gate portions of test implementation code using `SDK_INT` or |
| 105 | [`BuildCompat.isAtLeast`](https://developer.android.com/reference/androidx/core/os/BuildCompat) |
AndroidX Core Team | 25bc933 | 2021-08-10 11:11:26 -0700 | [diff] [blame] | 106 | methods. s To restrict to only physical devices, use |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 107 | [`@RequiresDevice`](https://developer.android.com/reference/androidx/test/filters/RequiresDevice). |
| 108 | |
AndroidX Core Team | 5c914c4 | 2021-02-08 17:22:57 +0000 | [diff] [blame] | 109 | NOTE [Cuttlefish](https://source.android.com/setup/create/cuttlefish) is not |
| 110 | affected by this annotation, only e.g. Studio emulators. If Cuttlefish is |
| 111 | displaying behavior that differs from a physical device, they are considering |
| 112 | that a bug in Cuttlefish, so please file those bugs instead of only looking for |
| 113 | a workaround. |
| 114 | |
AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 115 | ### Animations in tests {#animations} |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 116 | |
| 117 | Animations are disabled for tests by default. This helps avoid flakes due to |
| 118 | timing and also makes tests faster. |
| 119 | |
| 120 | In rare cases, like testing the animations themselves, you may want to enable |
| 121 | animations for a particular test or test class. For those cases, you can use the |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 122 | [`AnimationDurationScaleRule`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:testutils/testutils-runtime/src/main/java/androidx/testutils/AnimationDurationScaleRule.kt). |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 123 | |
AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 124 | ### Robolectric {#robolectric} |
alanv | f21d4ab | 2021-08-18 07:43:40 -0700 | [diff] [blame] | 125 | |
| 126 | Robolectric tests are supported in AndroidX; however, if you targeting a |
| 127 | pre-release version of the Android SDK then you may see an error like |
| 128 | |
| 129 | ``` |
| 130 | java.lang.IllegalArgumentException: Package targetSdkVersion=31 > maxSdkVersion=30 |
| 131 | at org.robolectric.plugins.DefaultSdkPicker.configuredSdks(DefaultSdkPicker.java:118) |
| 132 | at org.robolectric.plugins.DefaultSdkPicker.selectSdks(DefaultSdkPicker.java:69) |
| 133 | ``` |
| 134 | |
| 135 | You can force Robolectric to run using an earlier version of the platform SDK by |
| 136 | creating a `<project>/src/test/resources/robolectric.properties` file with the |
| 137 | following contents: |
| 138 | |
| 139 | ``` |
alanv | df3e8b9 | 2021-11-30 13:13:05 -0800 | [diff] [blame] | 140 | # Robolectric currently doesn't support API 31, so we have to explicitly specify 30 as the target |
alanv | f21d4ab | 2021-08-18 07:43:40 -0700 | [diff] [blame] | 141 | # sdk for now. Remove when no longer necessary. |
alanv | df3e8b9 | 2021-11-30 13:13:05 -0800 | [diff] [blame] | 142 | sdk=30 |
alanv | f21d4ab | 2021-08-18 07:43:40 -0700 | [diff] [blame] | 143 | ``` |
| 144 | |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 145 | ## Using the emulator {#emulator} |
| 146 | |
| 147 | You can use the emulator or a real device to run tests. If you wish to use the |
| 148 | emulator, you will need to access the AVD Manager (and your downloaded emulator |
| 149 | images) using a separate "normal" instance of Android Studio. "Normal" means a |
| 150 | non-Canary build of Studio that you would use for regular app development -- the |
| 151 | important part being that it points to the Android SDK where your downloaded |
| 152 | emulator images reside. You will need to open a project to get the Tools menu -- |
| 153 | do NOT open the AndroidX project in the "normal" instance of Android Studio; |
| 154 | instead, open a normal app or create a blank project using the app wizard. |
| 155 | |
AndroidX Core Team | 4cc85fa | 2021-11-23 15:58:34 +0000 | [diff] [blame] | 156 | NOTE You can reuse the emulator and system images from a "normal" installation |
| 157 | of Android Studio by linking the `emulator` and `system_images` directories to a |
AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 158 | standard Android SDK path and restarting Android Studio. **This is set up |
| 159 | automatically by `studiow` on Google-managed devices with a standard Android SDK |
| 160 | path.** In other cases, it may be set up manually with something like: `cd |
AndroidX Core Team | 4cc85fa | 2021-11-23 15:58:34 +0000 | [diff] [blame] | 161 | prebuilts/fullsdk-darwin ln -s ~/Library/Android/sdk/emulator emulator ln -s |
AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 162 | ~/Library/Android/sdk/system-images system-images` (substituting `fullsdk-linux` |
| 163 | and your local SDK path as appropriate) |
AndroidX Core Team | 4cc85fa | 2021-11-23 15:58:34 +0000 | [diff] [blame] | 164 | |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 165 | ## Debugging with platform SDK sources {#sources} |
| 166 | |
| 167 | The platform SDK sources that are checked into the development branch may not |
| 168 | match up with the build of Android present on the emulator or your physical |
| 169 | device. As a result, the line numbers reported by the debugger may not match up |
| 170 | the actual code being run. |
| 171 | |
| 172 | If you have a copy of the sources for the build against which you are debugging, |
| 173 | you can manually specify your platform SDK source path: |
| 174 | |
| 175 | 1. Click on a module (e.g. `appcompat`) in the `Project` view |
| 176 | 1. Press `Ctrl-Shift-A` and type "Module Settings", then run the action |
| 177 | 1. In the `Project Structure` dialog, navigate to `SDKs > Android API 29 |
| 178 | Platform > Sourcepath` |
| 179 | 1. Use the `-` button to remove any paths that are present, then use the `+` |
| 180 | button to add the desired source path, ex. `<android checkout |
| 181 | root>/frameworks/base` if you are debugging against a locally-built system |
| 182 | image |
| 183 | |
| 184 | NOTE The `Project Structure` dialog reachable via `File > Project Structure` is |
| 185 | **not** the same as the `Project Structure` dialog that will allow you to |
| 186 | specify the SDK source path. You must use the "Module Settings" action as |
| 187 | directed above. |
| 188 | |
| 189 | ## Running unit and integration tests {#running} |
| 190 | |
| 191 | From Android Studio, right-click can be used to run most test targets, including |
| 192 | source files, classes within a file, or individual test methods but **not** |
| 193 | entire modules. To run a supported test target, right-click on the test target |
| 194 | and then click `Run <name of test target>`. |
| 195 | |
| 196 | To run tests for an entire module such as `appcompat`, use `Run -> Edit |
| 197 | configurations...` and use the `+` button to create a new `Android Instrumented |
| 198 | Tests` configuration. Specify the module to be tested, give it a reasonable name |
| 199 | (not "All Tests") and click `OK`, then use the `Run` menu to run the |
| 200 | configuration. |
| 201 | |
| 202 |  |
| 203 | |
| 204 | NOTE If you receive the error `JUnit version 3.8 or later expected` this means |
| 205 | that Android Studio generated an Android JUnit configuration when you actually |
| 206 | needed an Android Instrumented Tests configuration. Open the `Run -> Edit |
| 207 | configurations...` dialog and delete the configuration from Android JUnit, then |
| 208 | manually add a configuration in Android Instrumented Tests. |
| 209 | |
| 210 | ### From the command line {#running-from-shell} |
| 211 | |
| 212 | Following a successful build, tests may be run against a particular AndroidX |
| 213 | module using `gradlew`. |
| 214 | |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 215 | To run all unit or integration tests in a specific project, run the following |
| 216 | from `framework/support`: |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 217 | |
| 218 | ```shell |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 219 | # Run instrumentation tests on a connected device |
| 220 | ./gradlew <project-name>:connectedAndroidTest --info --daemon |
| 221 | |
| 222 | # Run local unit tests |
| 223 | ./gradlew <project-name>:test --info --daemon |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 224 | ``` |
| 225 | |
| 226 | substituting the Gradle project name (ex. `core`). |
| 227 | |
| 228 | To run all integration tests in the specific project and test class you're |
| 229 | working on, run |
| 230 | |
| 231 | ```shell |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 232 | ./gradlew <project-name>:connectedAndroidTest --info --daemon \ |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 233 | -Pandroid.testInstrumentationRunnerArguments.class=<fully-qualified-class>[\#testName] |
| 234 | ``` |
| 235 | |
| 236 | substituting the Gradle project name (ex. `viewpager`) and fully-qualified class |
| 237 | name (ex. `androidx.viewpager.widget.ViewPagerTest`) of your test file, |
| 238 | optionally followed by `\#testName` if you want to execute a single test in that |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 239 | file. Substitute `test` for `connectedAndroidTest` to run local unit tests. |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 240 | |
| 241 | If you see some weird compilation errors such as below, run `./gradlew clean` |
| 242 | first: |
| 243 | |
| 244 | ``` |
| 245 | Unknown source file : UNEXPECTED TOP-LEVEL EXCEPTION: |
| 246 | Unknown source file : com.android.dex.DexException: Multiple dex files define Landroid/content/pm/ParceledListSlice$1; |
| 247 | ``` |
| 248 | |
| 249 | ## Test apps {#testapps} |
| 250 | |
| 251 | Library developers are strongly encouraged to write test apps that exercise |
| 252 | their library's public API surface. Test apps serve multiple purposes: |
| 253 | |
| 254 | * Integration testing and validation of API testability, when paired with |
| 255 | tests |
| 256 | * Validation of API usability and developer experience, when paired with a use |
| 257 | case or critical user journey |
| 258 | * Sample documentation, when embedded into API reference docs using the |
| 259 | [`@sample` and `@Sampled` annotations](api_guidelines.md#sample-usage) |
| 260 | |
| 261 | ### Legacy test apps {#testapps-legacy} |
| 262 | |
| 263 | We have a set of legacy sample Android applications in projects suffixed with |
| 264 | `-demos`. These applications do not have tests and should not be used as test |
| 265 | apps for new APIs, but they may be useful for manual regression testing. |
| 266 | |
| 267 | 1. Click `Run/Debug Configuration` on the top of the window. |
| 268 | 1. Select the app you want to run. |
| 269 | 1. Click 'Run' button. |
| 270 | |
| 271 |  |
| 272 | |
| 273 | ## Benchmarking {#benchmarking} |
| 274 | |
| 275 | AndroidX supports benchmarking - locally with Studio/Gradle, and continuously in |
| 276 | post-submit. For more information on how to create and run benchmarks, see |
| 277 | [Benchmarking](benchmarking.md). |