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 | |
| 27 | ### What gets tested, and when |
| 28 | |
| 29 | We use the |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 30 | [AffectedModuleDetector](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:buildSrc/src/main/kotlin/androidx/build/dependencyTracker/AffectedModuleDetector.kt) |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 31 | to determine what projects have changed since the last merge. |
| 32 | |
| 33 | In presubmit, "affected" modules will run all host and device tests regardless |
| 34 | of size. Modules that _depend_ on affected modules will run all host tests, but |
| 35 | will only run device tests annotated with `@SmallTest` or `@MediumTest`. |
| 36 | |
| 37 | When changes are made that can't be associated with a module, are in the root of |
| 38 | the checkout, or are within `buildSrc`, then all host tests and all device tests |
| 39 | annotated with `@SmallTest` or `@MediumTest` will be run for all modules. |
| 40 | |
| 41 | Presubmit tests represent only a subset of the devices on which our tests run. |
| 42 | The remaining devices are tested only in postsubmit. In postsubmit, all host and |
| 43 | device tests are run for all modules. |
| 44 | |
| 45 | ### Test annotations |
| 46 | |
| 47 | #### Test size |
| 48 | |
| 49 | All device tests *should* be given a size annotation, which is one of: |
| 50 | |
| 51 | * [`@SmallTest`](https://developer.android.com/reference/androidx/test/filters/SmallTest) |
| 52 | * [`@MediumTest`](https://developer.android.com/reference/androidx/test/filters/MediumTest) |
| 53 | * [`@LargeTest`](https://developer.android.com/reference/androidx/test/filters/LargeTest) |
| 54 | |
| 55 | If a device test is _not_ annotated with its size, it will be considered a |
| 56 | `@LargeTest` by default. Host tests do not need to be annotated with their size, |
| 57 | as all host tests are run regardless of size. |
| 58 | |
| 59 | This annotation can occur at either the class level or individual test level. |
| 60 | After API level 27, timeouts are enforced based on this annotation. |
| 61 | |
| 62 | Annotation | Max duration | Timeout after |
| 63 | ------------- | ------------ | ------------- |
| 64 | `@SmallTest` | 200ms | 300ms |
| 65 | `@MediumTest` | 1000ms | 1500ms |
| 66 | `@LargeTest` | 100000ms | 120000ms |
| 67 | |
| 68 | Small tests should be less than 200ms, and the timeout is set to 300ms. Medium |
| 69 | tests should be less than 1000ms, and the timeout is set to 1500ms. Large tests |
| 70 | have a timeout of 120000ms, which should cover any remaining tests. |
| 71 | |
| 72 | The exception to this rule is when using a runner other than |
| 73 | [`AndroidJUnitRunner`](https://developer.android.com/training/testing/junit-runner). |
| 74 | Since these runners do not enforce timeouts, tests that use them must not use a |
| 75 | size annotation. They will run whenever large tests would run. |
| 76 | |
| 77 | Currently the only allowed alternative is the |
| 78 | [`Parameterized`](https://junit.org/junit4/javadoc/4.12/org/junit/runners/Parameterized.html) |
| 79 | runner. If you need to use a different runner for some reason, please reach out |
| 80 | to the team and we can review/approve the use. |
| 81 | |
| 82 | #### Disabling tests |
| 83 | |
| 84 | To disable a device-side test in presubmit testing only -- but still have it run |
| 85 | in postsubmit -- use the |
| 86 | [`@FlakyTest`](https://developer.android.com/reference/androidx/test/filters/FlakyTest) |
| 87 | annotation. There is currently no support for presubmit-only disabling of |
| 88 | host-side tests. |
| 89 | |
| 90 | If you need to stop a host- or device-side test from running entirely, use |
| 91 | JUnit's [`@Ignore`](http://junit.sourceforge.net/javadoc/org/junit/Ignore.html) |
| 92 | annotation. Do *not* use Android's `@Suppress` annotation, which only works with |
| 93 | Android test runners and will *not* work for host-side tests. |
| 94 | |
| 95 | #### Filtering devices |
| 96 | |
| 97 | To restrict a test to a range of SDKs, use |
| 98 | [`@SdkSuppress`](https://developer.android.com/reference/androidx/test/filters/SdkSuppress) |
| 99 | which allows specifying a range with `minSdkVersion` and `maxSdkVersion`. This |
| 100 | annotation also supports targeting a specific pre-release SDK with the |
| 101 | `codeName` parameter. |
| 102 | |
| 103 | ```java |
| 104 | // Target SDKs 17 through 19, inclusive |
| 105 | @SdkSuppress(minSdkVersion = 17, maxSdkVersion = 19) |
| 106 | |
| 107 | // Target pre-release SDK R only |
| 108 | @SdkSuppress(minSdkVersion = Build.VERSION_CODES.R, isCodeName = "R") |
| 109 | ``` |
| 110 | |
| 111 | You may also gate portions of test implementation code using `SDK_INT` or |
| 112 | [`BuildCompat.isAtLeast`](https://developer.android.com/reference/androidx/core/os/BuildCompat) |
| 113 | methods. |
| 114 | |
| 115 | To restrict to only phsyical devices, use |
| 116 | [`@RequiresDevice`](https://developer.android.com/reference/androidx/test/filters/RequiresDevice). |
| 117 | |
AndroidX Core Team | 5c914c4 | 2021-02-08 17:22:57 +0000 | [diff] [blame] | 118 | NOTE [Cuttlefish](https://source.android.com/setup/create/cuttlefish) is not |
| 119 | affected by this annotation, only e.g. Studio emulators. If Cuttlefish is |
| 120 | displaying behavior that differs from a physical device, they are considering |
| 121 | that a bug in Cuttlefish, so please file those bugs instead of only looking for |
| 122 | a workaround. |
| 123 | |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 124 | ### Animations in tests |
| 125 | |
| 126 | Animations are disabled for tests by default. This helps avoid flakes due to |
| 127 | timing and also makes tests faster. |
| 128 | |
| 129 | In rare cases, like testing the animations themselves, you may want to enable |
| 130 | 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] | 131 | [`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] | 132 | |
| 133 | ## Using the emulator {#emulator} |
| 134 | |
| 135 | You can use the emulator or a real device to run tests. If you wish to use the |
| 136 | emulator, you will need to access the AVD Manager (and your downloaded emulator |
| 137 | images) using a separate "normal" instance of Android Studio. "Normal" means a |
| 138 | non-Canary build of Studio that you would use for regular app development -- the |
| 139 | important part being that it points to the Android SDK where your downloaded |
| 140 | emulator images reside. You will need to open a project to get the Tools menu -- |
| 141 | do NOT open the AndroidX project in the "normal" instance of Android Studio; |
| 142 | instead, open a normal app or create a blank project using the app wizard. |
| 143 | |
| 144 | ## Debugging with platform SDK sources {#sources} |
| 145 | |
| 146 | The platform SDK sources that are checked into the development branch may not |
| 147 | match up with the build of Android present on the emulator or your physical |
| 148 | device. As a result, the line numbers reported by the debugger may not match up |
| 149 | the actual code being run. |
| 150 | |
| 151 | If you have a copy of the sources for the build against which you are debugging, |
| 152 | you can manually specify your platform SDK source path: |
| 153 | |
| 154 | 1. Click on a module (e.g. `appcompat`) in the `Project` view |
| 155 | 1. Press `Ctrl-Shift-A` and type "Module Settings", then run the action |
| 156 | 1. In the `Project Structure` dialog, navigate to `SDKs > Android API 29 |
| 157 | Platform > Sourcepath` |
| 158 | 1. Use the `-` button to remove any paths that are present, then use the `+` |
| 159 | button to add the desired source path, ex. `<android checkout |
| 160 | root>/frameworks/base` if you are debugging against a locally-built system |
| 161 | image |
| 162 | |
| 163 | NOTE The `Project Structure` dialog reachable via `File > Project Structure` is |
| 164 | **not** the same as the `Project Structure` dialog that will allow you to |
| 165 | specify the SDK source path. You must use the "Module Settings" action as |
| 166 | directed above. |
| 167 | |
| 168 | ## Running unit and integration tests {#running} |
| 169 | |
| 170 | From Android Studio, right-click can be used to run most test targets, including |
| 171 | source files, classes within a file, or individual test methods but **not** |
| 172 | entire modules. To run a supported test target, right-click on the test target |
| 173 | and then click `Run <name of test target>`. |
| 174 | |
| 175 | To run tests for an entire module such as `appcompat`, use `Run -> Edit |
| 176 | configurations...` and use the `+` button to create a new `Android Instrumented |
| 177 | Tests` configuration. Specify the module to be tested, give it a reasonable name |
| 178 | (not "All Tests") and click `OK`, then use the `Run` menu to run the |
| 179 | configuration. |
| 180 | |
| 181 |  |
| 182 | |
| 183 | NOTE If you receive the error `JUnit version 3.8 or later expected` this means |
| 184 | that Android Studio generated an Android JUnit configuration when you actually |
| 185 | needed an Android Instrumented Tests configuration. Open the `Run -> Edit |
| 186 | configurations...` dialog and delete the configuration from Android JUnit, then |
| 187 | manually add a configuration in Android Instrumented Tests. |
| 188 | |
| 189 | ### From the command line {#running-from-shell} |
| 190 | |
| 191 | Following a successful build, tests may be run against a particular AndroidX |
| 192 | module using `gradlew`. |
| 193 | |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 194 | To run all unit or integration tests in a specific project, run the following |
| 195 | from `framework/support`: |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 196 | |
| 197 | ```shell |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 198 | # Run instrumentation tests on a connected device |
| 199 | ./gradlew <project-name>:connectedAndroidTest --info --daemon |
| 200 | |
| 201 | # Run local unit tests |
| 202 | ./gradlew <project-name>:test --info --daemon |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 203 | ``` |
| 204 | |
| 205 | substituting the Gradle project name (ex. `core`). |
| 206 | |
| 207 | To run all integration tests in the specific project and test class you're |
| 208 | working on, run |
| 209 | |
| 210 | ```shell |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 211 | ./gradlew <project-name>:connectedAndroidTest --info --daemon \ |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 212 | -Pandroid.testInstrumentationRunnerArguments.class=<fully-qualified-class>[\#testName] |
| 213 | ``` |
| 214 | |
| 215 | substituting the Gradle project name (ex. `viewpager`) and fully-qualified class |
| 216 | name (ex. `androidx.viewpager.widget.ViewPagerTest`) of your test file, |
| 217 | 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] | 218 | file. Substitute `test` for `connectedAndroidTest` to run local unit tests. |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 219 | |
| 220 | If you see some weird compilation errors such as below, run `./gradlew clean` |
| 221 | first: |
| 222 | |
| 223 | ``` |
| 224 | Unknown source file : UNEXPECTED TOP-LEVEL EXCEPTION: |
| 225 | Unknown source file : com.android.dex.DexException: Multiple dex files define Landroid/content/pm/ParceledListSlice$1; |
| 226 | ``` |
| 227 | |
| 228 | ## Test apps {#testapps} |
| 229 | |
| 230 | Library developers are strongly encouraged to write test apps that exercise |
| 231 | their library's public API surface. Test apps serve multiple purposes: |
| 232 | |
| 233 | * Integration testing and validation of API testability, when paired with |
| 234 | tests |
| 235 | * Validation of API usability and developer experience, when paired with a use |
| 236 | case or critical user journey |
| 237 | * Sample documentation, when embedded into API reference docs using the |
| 238 | [`@sample` and `@Sampled` annotations](api_guidelines.md#sample-usage) |
| 239 | |
| 240 | ### Legacy test apps {#testapps-legacy} |
| 241 | |
| 242 | We have a set of legacy sample Android applications in projects suffixed with |
| 243 | `-demos`. These applications do not have tests and should not be used as test |
| 244 | apps for new APIs, but they may be useful for manual regression testing. |
| 245 | |
| 246 | 1. Click `Run/Debug Configuration` on the top of the window. |
| 247 | 1. Select the app you want to run. |
| 248 | 1. Click 'Run' button. |
| 249 | |
| 250 |  |
| 251 | |
| 252 | ## Benchmarking {#benchmarking} |
| 253 | |
| 254 | AndroidX supports benchmarking - locally with Studio/Gradle, and continuously in |
| 255 | post-submit. For more information on how to create and run benchmarks, see |
| 256 | [Benchmarking](benchmarking.md). |