| 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 | |
| AndroidX Core Team | aa5f657 | 2023-11-28 08:39:09 -0800 | [diff] [blame^] | 10 | ## Motivation |
| 11 | |
| 12 | Jetpack libraries are developed with the intention that they are functionally |
| 13 | stable and production-ready as of the first public `alpha01` release, and that |
| 14 | they remain production-ready at tip-of-tree thereafter. |
| 15 | |
| 16 | For this reason, we emphasize that continuous integration testing -- both pre- |
| 17 | and post-submit -- is the ultimate source of truth for library correctness. If |
| 18 | tests are failing at head, the library is not only at risk of blocking public |
| 19 | releases but at risk of breaking production Google apps that rely on its |
| 20 | tip-of-tree builds. |
| 21 | |
| 22 | ### API level coverage in CI |
| 23 | |
| 24 | Generally, we aim to test Jetpack libraries against (1) the earliest supported |
| 25 | API level, (2) the latest stable API level, (3) API levels with major changes, |
| 26 | (4) API levels with high concentration of devices in the field, and (5) the next |
| 27 | pre-release API level. |
| 28 | |
| 29 | In practice, this is limited by device and emulator availability and |
| 30 | reliability. As of November 2023, we run tests on the following API levels: |
| 31 | |
| 32 | - API level 21: the lowest API level supported by Firebase Test Lab (FTL) |
| 33 | - API level 26: the lowest supported ARM-based emulator FTL runner, which has |
| 34 | much greater performance and stability |
| 35 | - API level 28: provides coverage between 26 and 30 |
| 36 | - API levels 30, 31, 33: the latest supported API levels, which represent the |
| 37 | majority of devices in the field |
| 38 | |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 39 | ## Adding tests {#adding} |
| 40 | |
| 41 | For an example of how to set up simple unit and integration tests in a new |
| 42 | module, see |
| 43 | [aosp/1189799](https://android-review.googlesource.com/c/platform/frameworks/support/+/1189799). |
| 44 | For an example of how to set up Espresso-powered integration tests, see the |
| 45 | `preference` library's |
| AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 46 | [`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] | 47 | and |
| AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 48 | [`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] | 49 | files. |
| 50 | |
| 51 | The currently allowed test runners for on-device tests are |
| 52 | [`AndroidJUnitRunner`](https://developer.android.com/training/testing/junit-runner) |
| 53 | and |
| 54 | [`Parameterized`](https://junit.org/junit4/javadoc/4.12/org/junit/runners/Parameterized.html). |
| 55 | |
| AndroidX Core Team | b5ba61d | 2021-06-08 09:20:36 -0700 | [diff] [blame] | 56 | NOTE All package/class/method combinations must be unique. Multiple copies of |
| 57 | the same class/method can be included e.g. under different directories, but must |
| 58 | be distinguishable by their packages. |
| 59 | |
| AndroidX Core Team | 03b4da3 | 2021-03-10 23:20:41 +0000 | [diff] [blame] | 60 | NOTE For best practices on writing libraries in a way that makes it easy for end |
| 61 | users -- and library developers -- to write tests, see the |
| Ian Baker | 186108e | 2023-11-20 06:54:36 -0800 | [diff] [blame] | 62 | [Testability](/docs/testability.md) guide. |
| AndroidX Core Team | 03b4da3 | 2021-03-10 23:20:41 +0000 | [diff] [blame] | 63 | |
| AndroidX Core Team | 5330eef | 2023-02-21 16:07:59 -0500 | [diff] [blame] | 64 | ### Adding a JVM based screenshot test |
| 65 | |
| 66 | For UI heavy libraries, it might make sense to add screenshot tests to verify |
| 67 | that everything still renders as expected. For that you need to write the test |
| 68 | ([example](https://r.android.com/2428035)) and add new goldens |
| 69 | ([example](https://r.android.com/2428721)). You can run these tests just like |
| 70 | any other JVM test using `test` Gradle task. |
| 71 | |
| AndroidX Core Team | e11d093 | 2023-09-08 09:43:38 -0700 | [diff] [blame] | 72 | ### Adding screenshots tests using scuba library |
| 73 | |
| 74 | #### Prerequisites |
| 75 | |
| 76 | Golden project: Make sure that you have the golden directory in your root |
| 77 | checkout (sibling of frameworks directory). If not re-init your repo to fetch |
| 78 | the latest manifest file: |
| 79 | |
| 80 | ``` |
| 81 | $ repo init -u sso://android/platform/manifest \ |
| 82 | -b androidx-main && repo sync -c -j8 |
| 83 | ``` |
| 84 | |
| 85 | Set up your module: If your module is not using screenshot tests yet, you need |
| 86 | to do the initial setup. |
| 87 | |
| 88 | 1. Modify your gradle file: Add dependency on the diffing library into your |
| 89 | gradle file: |
| 90 | |
| 91 | ``` |
| 92 | androidTestImplementation project(“:test:screenshot:screenshot”) |
| 93 | ``` |
| 94 | |
| 95 | Important step: Add golden asset directory to be linked to your test apk: |
| 96 | |
| 97 | ``` |
| 98 | android { |
| 99 | sourceSets.androidTest.assets.srcDirs += |
| 100 | // For androidx project (not in ui dir) use "/../../golden/project" |
| 101 | project.rootDir.absolutePath + "/../../golden/compose/material/material" |
| 102 | } |
| 103 | ``` |
| 104 | |
| 105 | This will bundle the goldens into your apk so they can be retrieved during |
| 106 | the test. |
| 107 | |
| 108 | 2. Create directory and variable: In the golden directory, create a new |
| 109 | directory for your module (the directory that you added to your gradle file, |
| 110 | which in case of material was “compose/material/material”). |
| 111 | |
| 112 | In your test module, create a variable pointing at your new directory: |
| 113 | |
| 114 | ``` |
| 115 | const val GOLDEN_MATERIAL = "compose/material/material" |
| 116 | ``` |
| 117 | |
| 118 | #### Adding a screenshot test |
| 119 | |
| 120 | Here is an example of a minimal screenshot test for compose material. |
| 121 | |
| 122 | ``` |
| 123 | @LargeTest |
| 124 | @RunWith(JUnit4::class) |
| 125 | @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O) |
| 126 | class CheckboxScreenshotTest { |
| 127 | @get:Rule val composeTestRule = createComposeRule() |
| 128 | @get:Rule val screenshotRule = AndroidXScreenshotTestRule(GOLDEN_MATERIAL) |
| 129 | |
| 130 | @Test |
| 131 | fun checkBoxTest_checked() { |
| 132 | composeTestRule.setMaterialContent { |
| 133 | Checkbox(Modifier.wrapContentSize(Alignment.TopStart), |
| 134 | checked = true, |
| 135 | onCheckedChange = {} |
| 136 | ) |
| 137 | } |
| 138 | find(isToggleable()) |
| 139 | .captureToBitmap() |
| 140 | .assertAgainstGolden(screenshotRule, "checkbox_checked") |
| 141 | } |
| 142 | } |
| 143 | ``` |
| 144 | |
| 145 | NOTE: The string “checkbox_checked” is the unique identifier of your golden in |
| 146 | your module. We use that string to name the golden file so avoid special |
| 147 | characters. Please avoid any substrings like: golden, image etc. as there is no |
| 148 | need - instead just describe what the image contains. |
| 149 | |
| 150 | #### Guidance around diffing |
| 151 | |
| 152 | Try to take the smallest screenshot possible. This will reduce interference from |
| 153 | other elements. |
| 154 | |
| 155 | By default we use a MSSIM comparer. This one is based on similarity. However we |
| 156 | have quite a high bar currently which is 0.98 (1 is an exact match). You can |
| 157 | provide your own threshold or even opt into a pixel perfect comparer for some |
| 158 | reason. |
| 159 | |
| 160 | Note: The bigger screenshots you take the more you sacrifice in the precision as |
| 161 | you can aggregate larger diffing errors, see the examples below. |
| 162 | |
| 163 |  |
| 164 | |
| 165 | #### Generating your goldens in CI (Gerrit) |
| 166 | |
| 167 | Upload your CL to gerrit and run presubmit. You should see your test fail. |
| 168 | |
| 169 | Step 1: Click on the “Test” button below: |
| 170 | |
| 171 |  |
| 172 | |
| 173 | Step 2: Click on the “Update scuba goldens” below: |
| 174 |  |
| 175 | |
| 176 | Step 3: You should see a dashboard similar to the example below. Check-out if |
| 177 | the new screenshots look as expected and if yes click approve. This will create |
| 178 | a new CL. |
| 179 |  |
| 180 | |
| 181 | Step 4: Link your original CL with the new goldens CL by setting the same Topic |
| 182 | field in both CLs (any arbitrary string will do). This tells Gerrit to submit |
| 183 | the CLs together, effectively providing a reference from the original CL to the |
| 184 | new goldens. And re-run presubmit. Your tests should now pass! |
| 185 |  |
| 186 | |
| 187 | #### Running manually / debugging |
| 188 | |
| 189 | Screenshot tests can be run locally using pixel 2 api33 emulator. Start the |
| 190 | emulator using [these](#emulator) steps. |
| 191 | |
| 192 | Wait until the emulator is running and run the tests as you would on a regular |
| 193 | device. |
| 194 | |
| 195 | ``` |
| 196 | $ ./gradlew <module>:cAT -Pandroid.testInstrumentationRunnerArguments.class=<class> |
| 197 | ``` |
| 198 | |
| 199 | If the test passes, the results are limited to a .textproto file for each |
| 200 | screenshot test. If the test fails, the results will also contain the actual |
| 201 | screenshot and, if available, the golden reference image and the diff between |
| 202 | the two. Note that this means that if you want to regenerate the golden image, |
| 203 | you have to remove the golden image before running the test. |
| 204 | |
| 205 | To get the screenshot related results from the device onto your workstation, you |
| 206 | can run |
| 207 | |
| 208 | ``` |
| 209 | $ adb pull /sdcard/Android/data/<test-package>/cache/androidx_screenshots |
| 210 | ``` |
| 211 | |
| 212 | where test-package is the identifier of you test apk, e.g. |
| 213 | androidx.compose.material.test |
| 214 | |
| 215 | #### Locally updating the golden images |
| 216 | |
| 217 | After you run a screenshot test and pull the results to a desired location, |
| 218 | verify that the actual images are the correct ones and copy them to the golden |
| 219 | screenshots directory (the one you use to create the AndroidXScreenshotTestRule |
| 220 | with) using this script. |
| 221 | |
| 222 | ``` |
| 223 | androidx-main/frameworks/support/development/copy_screenshots_to_golden_repo.py \ |
| 224 | --input-dir=/tmp/androidx_screenshots/ --output-dir=androidx-main/golden/<test>/ |
| 225 | ``` |
| 226 | |
| 227 | Repeat for all screenshots, then create and upload a CL in the golden |
| 228 | repository. |
| 229 | |
| AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 230 | ### What gets tested, and when {#affected-module-detector} |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 231 | |
| AndroidX Core Team | 3da6263 | 2022-10-03 11:29:25 -0700 | [diff] [blame] | 232 | With over 45000 tests executed on every CI run, it is necessary for us to run |
| 233 | only a subset of our instrumentation tests in presubmit. We use the |
| AndroidX Core Team | 4cc85fa | 2021-11-23 15:58:34 +0000 | [diff] [blame] | 234 | [AffectedModuleDetector](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:buildSrc/private/src/main/kotlin/androidx/build/dependencyTracker/AffectedModuleDetector.kt) |
| AndroidX Core Team | 3da6263 | 2022-10-03 11:29:25 -0700 | [diff] [blame] | 235 | to determine what projects have changed since the last merge. In turn, we only |
| 236 | generate apks and test configurations for those changed modules and their |
| 237 | dependencies. |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 238 | |
| 239 | When changes are made that can't be associated with a module, are in the root of |
| 240 | the checkout, or are within `buildSrc`, then all host tests and all device tests |
| 241 | annotated with `@SmallTest` or `@MediumTest` will be run for all modules. |
| 242 | |
| 243 | Presubmit tests represent only a subset of the devices on which our tests run. |
| 244 | The remaining devices are tested only in postsubmit. In postsubmit, all host and |
| 245 | device tests are run for all modules. |
| 246 | |
| AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 247 | ### Test annotations {#annotations} |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 248 | |
| AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 249 | #### Test size and runners {#test-size} |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 250 | |
| 251 | All device tests *should* be given a size annotation, which is one of: |
| 252 | |
| 253 | * [`@SmallTest`](https://developer.android.com/reference/androidx/test/filters/SmallTest) |
| 254 | * [`@MediumTest`](https://developer.android.com/reference/androidx/test/filters/MediumTest) |
| 255 | * [`@LargeTest`](https://developer.android.com/reference/androidx/test/filters/LargeTest) |
| 256 | |
| alanv | 37fed3a2 | 2021-09-17 07:46:47 -0700 | [diff] [blame] | 257 | 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] | 258 | `@LargeTest` by default. Host tests do not need to be annotated with their size, |
| 259 | as all host tests are run regardless of size. |
| 260 | |
| 261 | 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] | 262 | |
| AndroidX Core Team | b5ba61d | 2021-06-08 09:20:36 -0700 | [diff] [blame] | 263 | Annotation | Max duration |
| 264 | ------------- | ------------ |
| 265 | `@SmallTest` | 200ms |
| 266 | `@MediumTest` | 1000ms |
| 267 | `@LargeTest` | 100000ms |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 268 | |
| AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 269 | #### Disabling tests {#disabling-tests} |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 270 | |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 271 | If you need to stop a host- or device-side test from running entirely, use |
| 272 | JUnit's [`@Ignore`](http://junit.sourceforge.net/javadoc/org/junit/Ignore.html) |
| 273 | annotation. Do *not* use Android's `@Suppress` annotation, which only works with |
| 274 | Android test runners and will *not* work for host-side tests. |
| 275 | |
| AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 276 | #### Filtering devices {#filtering-devices} |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 277 | |
| 278 | To restrict a test to a range of SDKs, use |
| 279 | [`@SdkSuppress`](https://developer.android.com/reference/androidx/test/filters/SdkSuppress) |
| 280 | which allows specifying a range with `minSdkVersion` and `maxSdkVersion`. This |
| 281 | annotation also supports targeting a specific pre-release SDK with the |
| 282 | `codeName` parameter. |
| 283 | |
| 284 | ```java |
| 285 | // Target SDKs 17 through 19, inclusive |
| 286 | @SdkSuppress(minSdkVersion = 17, maxSdkVersion = 19) |
| 287 | |
| AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 288 | // Target pre-release SDK T only |
| 289 | @SdkSuppress(minSdkVersion = Build.VERSION_CODES.TIRAMISU, codeName = "Tiramisu") |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 290 | ``` |
| 291 | |
| 292 | You may also gate portions of test implementation code using `SDK_INT` or |
| 293 | [`BuildCompat.isAtLeast`](https://developer.android.com/reference/androidx/core/os/BuildCompat) |
| AndroidX Core Team | 25bc933 | 2021-08-10 11:11:26 -0700 | [diff] [blame] | 294 | methods. s To restrict to only physical devices, use |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 295 | [`@RequiresDevice`](https://developer.android.com/reference/androidx/test/filters/RequiresDevice). |
| 296 | |
| AndroidX Core Team | 5c914c4 | 2021-02-08 17:22:57 +0000 | [diff] [blame] | 297 | NOTE [Cuttlefish](https://source.android.com/setup/create/cuttlefish) is not |
| 298 | affected by this annotation, only e.g. Studio emulators. If Cuttlefish is |
| 299 | displaying behavior that differs from a physical device, they are considering |
| 300 | that a bug in Cuttlefish, so please file those bugs instead of only looking for |
| 301 | a workaround. |
| 302 | |
| AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 303 | ### Animations in tests {#animations} |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 304 | |
| 305 | Animations are disabled for tests by default. This helps avoid flakes due to |
| 306 | timing and also makes tests faster. |
| 307 | |
| 308 | In rare cases, like testing the animations themselves, you may want to enable |
| 309 | 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] | 310 | [`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] | 311 | |
| AndroidX Core Team | 21ccf65 | 2022-04-01 14:53:07 +0000 | [diff] [blame] | 312 | ### Robolectric {#robolectric} |
| alanv | f21d4ab | 2021-08-18 07:43:40 -0700 | [diff] [blame] | 313 | |
| 314 | Robolectric tests are supported in AndroidX; however, if you targeting a |
| 315 | pre-release version of the Android SDK then you may see an error like |
| 316 | |
| 317 | ``` |
| alanv | 9102ecc | 2022-08-26 07:46:41 -0700 | [diff] [blame] | 318 | java.lang.IllegalArgumentException: Package targetSdkVersion=31 > maxSdkVersion=30 |
| alanv | f21d4ab | 2021-08-18 07:43:40 -0700 | [diff] [blame] | 319 | at org.robolectric.plugins.DefaultSdkPicker.configuredSdks(DefaultSdkPicker.java:118) |
| 320 | at org.robolectric.plugins.DefaultSdkPicker.selectSdks(DefaultSdkPicker.java:69) |
| 321 | ``` |
| 322 | |
| 323 | You can force Robolectric to run using an earlier version of the platform SDK by |
| 324 | creating a `<project>/src/test/resources/robolectric.properties` file with the |
| 325 | following contents: |
| 326 | |
| 327 | ``` |
| alanv | 9102ecc | 2022-08-26 07:46:41 -0700 | [diff] [blame] | 328 | # 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] | 329 | # sdk for now. Remove when no longer necessary. |
| alanv | 9102ecc | 2022-08-26 07:46:41 -0700 | [diff] [blame] | 330 | sdk=30 |
| alanv | f21d4ab | 2021-08-18 07:43:40 -0700 | [diff] [blame] | 331 | ``` |
| 332 | |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 333 | ## Using the emulator {#emulator} |
| 334 | |
| 335 | You can use the emulator or a real device to run tests. If you wish to use the |
| 336 | emulator, you will need to access the AVD Manager (and your downloaded emulator |
| 337 | images) using a separate "normal" instance of Android Studio. "Normal" means a |
| 338 | non-Canary build of Studio that you would use for regular app development -- the |
| 339 | important part being that it points to the Android SDK where your downloaded |
| 340 | emulator images reside. You will need to open a project to get the Tools menu -- |
| 341 | do NOT open the AndroidX project in the "normal" instance of Android Studio; |
| 342 | instead, open a normal app or create a blank project using the app wizard. |
| 343 | |
| AndroidX Core Team | 4cc85fa | 2021-11-23 15:58:34 +0000 | [diff] [blame] | 344 | NOTE You can reuse the emulator and system images from a "normal" installation |
| 345 | 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] | 346 | standard Android SDK path and restarting Android Studio. **This is set up |
| 347 | automatically by `studiow` on Google-managed devices with a standard Android SDK |
| 348 | 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] | 349 | 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] | 350 | ~/Library/Android/sdk/system-images system-images` (substituting `fullsdk-linux` |
| 351 | and your local SDK path as appropriate) |
| AndroidX Core Team | 4cc85fa | 2021-11-23 15:58:34 +0000 | [diff] [blame] | 352 | |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 353 | ## Debugging with platform SDK sources {#sources} |
| 354 | |
| 355 | The platform SDK sources that are checked into the development branch may not |
| 356 | match up with the build of Android present on the emulator or your physical |
| 357 | device. As a result, the line numbers reported by the debugger may not match up |
| 358 | the actual code being run. |
| 359 | |
| 360 | If you have a copy of the sources for the build against which you are debugging, |
| 361 | you can manually specify your platform SDK source path: |
| 362 | |
| 363 | 1. Click on a module (e.g. `appcompat`) in the `Project` view |
| 364 | 1. Press `Ctrl-Shift-A` and type "Module Settings", then run the action |
| 365 | 1. In the `Project Structure` dialog, navigate to `SDKs > Android API 29 |
| 366 | Platform > Sourcepath` |
| 367 | 1. Use the `-` button to remove any paths that are present, then use the `+` |
| 368 | button to add the desired source path, ex. `<android checkout |
| 369 | root>/frameworks/base` if you are debugging against a locally-built system |
| 370 | image |
| 371 | |
| 372 | NOTE The `Project Structure` dialog reachable via `File > Project Structure` is |
| 373 | **not** the same as the `Project Structure` dialog that will allow you to |
| 374 | specify the SDK source path. You must use the "Module Settings" action as |
| 375 | directed above. |
| 376 | |
| 377 | ## Running unit and integration tests {#running} |
| 378 | |
| 379 | From Android Studio, right-click can be used to run most test targets, including |
| 380 | source files, classes within a file, or individual test methods but **not** |
| 381 | entire modules. To run a supported test target, right-click on the test target |
| 382 | and then click `Run <name of test target>`. |
| 383 | |
| 384 | To run tests for an entire module such as `appcompat`, use `Run -> Edit |
| 385 | configurations...` and use the `+` button to create a new `Android Instrumented |
| 386 | Tests` configuration. Specify the module to be tested, give it a reasonable name |
| 387 | (not "All Tests") and click `OK`, then use the `Run` menu to run the |
| 388 | configuration. |
| 389 | |
| 390 |  |
| 391 | |
| 392 | NOTE If you receive the error `JUnit version 3.8 or later expected` this means |
| 393 | that Android Studio generated an Android JUnit configuration when you actually |
| 394 | needed an Android Instrumented Tests configuration. Open the `Run -> Edit |
| 395 | configurations...` dialog and delete the configuration from Android JUnit, then |
| 396 | manually add a configuration in Android Instrumented Tests. |
| 397 | |
| 398 | ### From the command line {#running-from-shell} |
| 399 | |
| 400 | Following a successful build, tests may be run against a particular AndroidX |
| 401 | module using `gradlew`. |
| 402 | |
| AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 403 | To run all unit or integration tests in a specific project, run the following |
| 404 | from `framework/support`: |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 405 | |
| 406 | ```shell |
| AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 407 | # Run instrumentation tests on a connected device |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 408 | ./gradlew <project-name>:connectedAndroidTest --info |
| 409 | |
| 410 | # Run instrumentation tests in Firebase Test Lab (remote) |
| 411 | ./gradlew <project-name>:ftlnexus4api21 |
| 412 | ./gradlew <project-name>:ftlpixel2api26 |
| 413 | ./gradlew <project-name>:ftlpixel2api28 |
| 414 | ./gradlew <project-name>:ftlpixel2api30 |
| 415 | ./gradlew <project-name>:ftlpixel2api33 |
| AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 416 | |
| 417 | # Run local unit tests |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 418 | ./gradlew <project-name>:test |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 419 | ``` |
| 420 | |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 421 | substituting the Gradle project name (ex. `:core:core`). |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 422 | |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 423 | To run a specific instrumentation test in a given project, run |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 424 | |
| 425 | ```shell |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 426 | # Run instrumentation tests on a connected device |
| 427 | ./gradlew <project-name>:connectedAndroidTest --info \ |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 428 | -Pandroid.testInstrumentationRunnerArguments.class=<fully-qualified-class>[\#testName] |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 429 | |
| 430 | # Run instrumentation tests on in Firebase Test Lab (remote) |
| 431 | ./gradlew <project-name>:ftlpixel2api30 --className=<fully-qualified-class> |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 432 | ``` |
| 433 | |
| 434 | substituting the Gradle project name (ex. `viewpager`) and fully-qualified class |
| 435 | name (ex. `androidx.viewpager.widget.ViewPagerTest`) of your test file, |
| 436 | optionally followed by `\#testName` if you want to execute a single test in that |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 437 | file |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 438 | |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 439 | If you want to run a specific unit test, you can do it using |
| AndroidX Core Team | a200cb8 | 2023-03-28 15:23:28 -0700 | [diff] [blame] | 440 | [`--tests` filtering](https://docs.gradle.org/current/userguide/java_testing.html#test_filtering): |
| 441 | |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 442 | ```shell |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 443 | # Run a test for an Android library on a connected device |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 444 | ./gradlew <project-name>:test --tests androidx.core.view.DisplayCompatTest |
| 445 | |
| 446 | # Run a test for a JVM library |
| AndroidX Core Team | 8c9a1c0 | 2023-03-08 14:16:36 -0800 | [diff] [blame] | 447 | ./gradlew <project-name>:testDebugUnitTest --tests |
| AndroidX Core Team | a200cb8 | 2023-03-28 15:23:28 -0700 | [diff] [blame] | 448 | androidx.core.view.DisplayCompatTest |
| 449 | ``` |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 450 | |
| 451 | ## Test apps {#testapps} |
| 452 | |
| 453 | Library developers are strongly encouraged to write test apps that exercise |
| 454 | their library's public API surface. Test apps serve multiple purposes: |
| 455 | |
| 456 | * Integration testing and validation of API testability, when paired with |
| 457 | tests |
| 458 | * Validation of API usability and developer experience, when paired with a use |
| 459 | case or critical user journey |
| 460 | * Sample documentation, when embedded into API reference docs using the |
| Ian Baker | 186108e | 2023-11-20 06:54:36 -0800 | [diff] [blame] | 461 | [`@sample` and `@Sampled` annotations](/docs/api_guidelines/index.md#sample-usage) |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 462 | |
| 463 | ### Legacy test apps {#testapps-legacy} |
| 464 | |
| 465 | We have a set of legacy sample Android applications in projects suffixed with |
| 466 | `-demos`. These applications do not have tests and should not be used as test |
| 467 | apps for new APIs, but they may be useful for manual regression testing. |
| 468 | |
| 469 | 1. Click `Run/Debug Configuration` on the top of the window. |
| 470 | 1. Select the app you want to run. |
| 471 | 1. Click 'Run' button. |
| 472 | |
| 473 |  |
| 474 | |
| 475 | ## Benchmarking {#benchmarking} |
| 476 | |
| 477 | AndroidX supports benchmarking - locally with Studio/Gradle, and continuously in |
| 478 | post-submit. For more information on how to create and run benchmarks, see |
| Ian Baker | 186108e | 2023-11-20 06:54:36 -0800 | [diff] [blame] | 479 | [Benchmarking](/docs/benchmarking.md). |