blob: b5b031fd5ba08a6559c69097eedcc1d697ac51f7 [file] [log] [blame] [view]
AndroidX Core Team2e416b22020-12-03 22:58:07 +00001# Testing
2
3[TOC]
4
5AndroidX contains unit and integration tests that are run automatically when a
6change is uploaded. It also contains a number of sample applications that are
7useful for demonstrating how to use features as well as performing manual
8testing.
9
AndroidX Core Teamaa5f6572023-11-28 08:39:09 -080010## Motivation
11
12Jetpack libraries are developed with the intention that they are functionally
13stable and production-ready as of the first public `alpha01` release, and that
14they remain production-ready at tip-of-tree thereafter.
15
16For this reason, we emphasize that continuous integration testing -- both pre-
17and post-submit -- is the ultimate source of truth for library correctness. If
18tests are failing at head, the library is not only at risk of blocking public
19releases but at risk of breaking production Google apps that rely on its
20tip-of-tree builds.
21
22### API level coverage in CI
23
24Generally, we aim to test Jetpack libraries against (1) the earliest supported
25API 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
27pre-release API level.
28
29In practice, this is limited by device and emulator availability and
30reliability. 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 Team2e416b22020-12-03 22:58:07 +000039## Adding tests {#adding}
40
41For an example of how to set up simple unit and integration tests in a new
42module, see
43[aosp/1189799](https://android-review.googlesource.com/c/platform/frameworks/support/+/1189799).
44For an example of how to set up Espresso-powered integration tests, see the
45`preference` library's
AndroidX Core Team408c27b2020-12-15 15:57:00 +000046[`build.gradle`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:preference/preference/build.gradle)
AndroidX Core Team2e416b22020-12-03 22:58:07 +000047and
AndroidX Core Team408c27b2020-12-15 15:57:00 +000048[`EditTextPreferenceTest.java`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:preference/preference/src/androidTest/java/androidx/preference/tests/EditTextPreferenceTest.java)
AndroidX Core Team2e416b22020-12-03 22:58:07 +000049files.
50
51The currently allowed test runners for on-device tests are
52[`AndroidJUnitRunner`](https://developer.android.com/training/testing/junit-runner)
53and
54[`Parameterized`](https://junit.org/junit4/javadoc/4.12/org/junit/runners/Parameterized.html).
55
AndroidX Core Teamb5ba61d2021-06-08 09:20:36 -070056NOTE All package/class/method combinations must be unique. Multiple copies of
57the same class/method can be included e.g. under different directories, but must
58be distinguishable by their packages.
59
AndroidX Core Team03b4da32021-03-10 23:20:41 +000060NOTE For best practices on writing libraries in a way that makes it easy for end
61users -- and library developers -- to write tests, see the
Ian Baker186108e2023-11-20 06:54:36 -080062[Testability](/docs/testability.md) guide.
AndroidX Core Team03b4da32021-03-10 23:20:41 +000063
AndroidX Core Teame11d0932023-09-08 09:43:38 -070064### Adding screenshots tests using scuba library
65
66#### Prerequisites
67
68Golden project: Make sure that you have the golden directory in your root
69checkout (sibling of frameworks directory). If not re-init your repo to fetch
70the latest manifest file:
71
72```
73$ repo init -u sso://android/platform/manifest \
74 -b androidx-main && repo sync -c -j8
75```
76
77Set up your module: If your module is not using screenshot tests yet, you need
78to do the initial setup.
79
801. Modify your gradle file: Add dependency on the diffing library into your
81 gradle file:
82
83 ```
84 androidTestImplementation project(“:test:screenshot:screenshot”)
85 ```
86
87 Important step: Add golden asset directory to be linked to your test apk:
88
89 ```
90 android {
91 sourceSets.androidTest.assets.srcDirs +=
92 // For androidx project (not in ui dir) use "/../../golden/project"
93 project.rootDir.absolutePath + "/../../golden/compose/material/material"
94 }
95 ```
96
97 This will bundle the goldens into your apk so they can be retrieved during
98 the test.
99
1002. Create directory and variable: In the golden directory, create a new
101 directory for your module (the directory that you added to your gradle file,
102 which in case of material was “compose/material/material”).
103
104 In your test module, create a variable pointing at your new directory:
105
106 ```
107 const val GOLDEN_MATERIAL = "compose/material/material"
108 ```
109
110#### Adding a screenshot test
111
112Here is an example of a minimal screenshot test for compose material.
113
114```
115@LargeTest
116@RunWith(JUnit4::class)
117@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
118class CheckboxScreenshotTest {
119 @get:Rule val composeTestRule = createComposeRule()
120 @get:Rule val screenshotRule = AndroidXScreenshotTestRule(GOLDEN_MATERIAL)
121
122 @Test
123 fun checkBoxTest_checked() {
124 composeTestRule.setMaterialContent {
125 Checkbox(Modifier.wrapContentSize(Alignment.TopStart),
126 checked = true,
127 onCheckedChange = {}
128 )
129 }
130 find(isToggleable())
131 .captureToBitmap()
132 .assertAgainstGolden(screenshotRule, "checkbox_checked")
133 }
134}
135```
136
137NOTE: The string “checkbox_checked” is the unique identifier of your golden in
138your module. We use that string to name the golden file so avoid special
139characters. Please avoid any substrings like: golden, image etc. as there is no
140need - instead just describe what the image contains.
141
142#### Guidance around diffing
143
144Try to take the smallest screenshot possible. This will reduce interference from
145other elements.
146
147By default we use a MSSIM comparer. This one is based on similarity. However we
148have quite a high bar currently which is 0.98 (1 is an exact match). You can
149provide your own threshold or even opt into a pixel perfect comparer for some
150reason.
151
152Note: The bigger screenshots you take the more you sacrifice in the precision as
153you can aggregate larger diffing errors, see the examples below.
154
155![alt_text](onboarding_images/image6.png "screenshot diff at different MSSIM")
156
157#### Generating your goldens in CI (Gerrit)
158
159Upload your CL to gerrit and run presubmit. You should see your test fail.
160
161Step 1: Click on the “Test” button below:
162
163![alt_text](onboarding_images/image7.png "Presubmit link to failed test")
164
165Step 2: Click on the “Update scuba goldens” below:
166![alt_text](onboarding_images/image8.png "Update scuba button")
167
AndroidX Core Teamadb58072024-07-18 11:08:51 -0700168Step 3: Select the tests for which you want to update the golden images. Confirm
169the images look correct and click on “Approve Changes”
AndroidX Core Teame11d0932023-09-08 09:43:38 -0700170![alt_text](onboarding_images/image9.png "Button to approve scuba changes")
171
AndroidX Core Teamadb58072024-07-18 11:08:51 -0700172Step 4: In the Approve changes dialog box, enter the following details and click
173on Approve: \
174Select gerrit host as shown in image below \
175Repo: platform/frameworks/support-golden \
176Branch: androidx-main
177![alt_text](onboarding_images/image10.png "Approve changes dialog box with dropdown field to select gerrit host and textboxes to select repo and branch")
178
179Step 5: Link your original CL with the new goldens CL by setting the same Topic
AndroidX Core Teame11d0932023-09-08 09:43:38 -0700180field in both CLs (any arbitrary string will do). This tells Gerrit to submit
181the CLs together, effectively providing a reference from the original CL to the
182new goldens. And re-run presubmit. Your tests should now pass!
AndroidX Core Teamadb58072024-07-18 11:08:51 -0700183![alt_text](onboarding_images/image11.png "Topic for connecting cls, so they can run together")
AndroidX Core Teame11d0932023-09-08 09:43:38 -0700184
185#### Running manually / debugging
186
187Screenshot tests can be run locally using pixel 2 api33 emulator. Start the
188emulator using [these](#emulator) steps.
189
190Wait until the emulator is running and run the tests as you would on a regular
191device.
192
193```
194$ ./gradlew <module>:cAT -Pandroid.testInstrumentationRunnerArguments.class=<class>
195```
196
197If the test passes, the results are limited to a .textproto file for each
198screenshot test. If the test fails, the results will also contain the actual
199screenshot and, if available, the golden reference image and the diff between
200the two. Note that this means that if you want to regenerate the golden image,
201you have to remove the golden image before running the test.
202
203To get the screenshot related results from the device onto your workstation, you
204can run
205
206```
207$ adb pull /sdcard/Android/data/<test-package>/cache/androidx_screenshots
208```
209
210where test-package is the identifier of you test apk, e.g.
211androidx.compose.material.test
212
213#### Locally updating the golden images
214
215After you run a screenshot test and pull the results to a desired location,
216verify that the actual images are the correct ones and copy them to the golden
217screenshots directory (the one you use to create the AndroidXScreenshotTestRule
218with) using this script.
219
220```
221androidx-main/frameworks/support/development/copy_screenshots_to_golden_repo.py \
222--input-dir=/tmp/androidx_screenshots/ --output-dir=androidx-main/golden/<test>/
223```
224
225Repeat for all screenshots, then create and upload a CL in the golden
226repository.
227
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000228### What gets tested, and when {#affected-module-detector}
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000229
AndroidX Core Team3da62632022-10-03 11:29:25 -0700230With over 45000 tests executed on every CI run, it is necessary for us to run
231only a subset of our instrumentation tests in presubmit. We use the
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +0000232[AffectedModuleDetector](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:buildSrc/private/src/main/kotlin/androidx/build/dependencyTracker/AffectedModuleDetector.kt)
AndroidX Core Team3da62632022-10-03 11:29:25 -0700233to determine what projects have changed since the last merge. In turn, we only
234generate apks and test configurations for those changed modules and their
235dependencies.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000236
237When changes are made that can't be associated with a module, are in the root of
238the checkout, or are within `buildSrc`, then all host tests and all device tests
239annotated with `@SmallTest` or `@MediumTest` will be run for all modules.
240
241Presubmit tests represent only a subset of the devices on which our tests run.
242The remaining devices are tested only in postsubmit. In postsubmit, all host and
243device tests are run for all modules.
244
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000245### Test annotations {#annotations}
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000246
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000247#### Test size and runners {#test-size}
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000248
249All device tests *should* be given a size annotation, which is one of:
250
251* [`@SmallTest`](https://developer.android.com/reference/androidx/test/filters/SmallTest)
252* [`@MediumTest`](https://developer.android.com/reference/androidx/test/filters/MediumTest)
253* [`@LargeTest`](https://developer.android.com/reference/androidx/test/filters/LargeTest)
254
alanv37fed3a22021-09-17 07:46:47 -0700255If a device test is *not* annotated with its size, it will be run as if it were
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000256`@LargeTest` by default. Host tests do not need to be annotated with their size,
257as all host tests are run regardless of size.
258
259This annotation can occur at either the class level or individual test level.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000260
AndroidX Core Teamb5ba61d2021-06-08 09:20:36 -0700261Annotation | Max duration
262------------- | ------------
263`@SmallTest` | 200ms
264`@MediumTest` | 1000ms
265`@LargeTest` | 100000ms
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000266
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000267#### Disabling tests {#disabling-tests}
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000268
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000269If you need to stop a host- or device-side test from running entirely, use
270JUnit's [`@Ignore`](http://junit.sourceforge.net/javadoc/org/junit/Ignore.html)
271annotation. Do *not* use Android's `@Suppress` annotation, which only works with
272Android test runners and will *not* work for host-side tests.
273
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000274#### Filtering devices {#filtering-devices}
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000275
276To restrict a test to a range of SDKs, use
277[`@SdkSuppress`](https://developer.android.com/reference/androidx/test/filters/SdkSuppress)
278which allows specifying a range with `minSdkVersion` and `maxSdkVersion`. This
279annotation also supports targeting a specific pre-release SDK with the
280`codeName` parameter.
281
282```java
283// Target SDKs 17 through 19, inclusive
284@SdkSuppress(minSdkVersion = 17, maxSdkVersion = 19)
285
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000286// Target pre-release SDK T only
287@SdkSuppress(minSdkVersion = Build.VERSION_CODES.TIRAMISU, codeName = "Tiramisu")
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000288```
289
290You may also gate portions of test implementation code using `SDK_INT` or
291[`BuildCompat.isAtLeast`](https://developer.android.com/reference/androidx/core/os/BuildCompat)
AndroidX Core Team25bc9332021-08-10 11:11:26 -0700292methods. s To restrict to only physical devices, use
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000293[`@RequiresDevice`](https://developer.android.com/reference/androidx/test/filters/RequiresDevice).
294
AndroidX Core Team5c914c42021-02-08 17:22:57 +0000295NOTE [Cuttlefish](https://source.android.com/setup/create/cuttlefish) is not
296affected by this annotation, only e.g. Studio emulators. If Cuttlefish is
297displaying behavior that differs from a physical device, they are considering
298that a bug in Cuttlefish, so please file those bugs instead of only looking for
299a workaround.
300
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000301### Animations in tests {#animations}
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000302
303Animations are disabled for tests by default. This helps avoid flakes due to
304timing and also makes tests faster.
305
306In rare cases, like testing the animations themselves, you may want to enable
307animations for a particular test or test class. For those cases, you can use the
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000308[`AnimationDurationScaleRule`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:testutils/testutils-runtime/src/main/java/androidx/testutils/AnimationDurationScaleRule.kt).
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000309
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000310### Robolectric {#robolectric}
alanvf21d4ab2021-08-18 07:43:40 -0700311
312Robolectric tests are supported in AndroidX; however, if you targeting a
313pre-release version of the Android SDK then you may see an error like
314
315```
alanv9102ecc2022-08-26 07:46:41 -0700316java.lang.IllegalArgumentException: Package targetSdkVersion=31 > maxSdkVersion=30
alanvf21d4ab2021-08-18 07:43:40 -0700317at org.robolectric.plugins.DefaultSdkPicker.configuredSdks(DefaultSdkPicker.java:118)
318at org.robolectric.plugins.DefaultSdkPicker.selectSdks(DefaultSdkPicker.java:69)
319```
320
321You can force Robolectric to run using an earlier version of the platform SDK by
322creating a `<project>/src/test/resources/robolectric.properties` file with the
323following contents:
324
325```
alanv9102ecc2022-08-26 07:46:41 -0700326# Robolectric currently doesn't support API 31, so we have to explicitly specify 30 as the target
alanvf21d4ab2021-08-18 07:43:40 -0700327# sdk for now. Remove when no longer necessary.
alanv9102ecc2022-08-26 07:46:41 -0700328sdk=30
alanvf21d4ab2021-08-18 07:43:40 -0700329```
330
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000331## Using the emulator {#emulator}
332
333You can use the emulator or a real device to run tests. If you wish to use the
334emulator, you will need to access the AVD Manager (and your downloaded emulator
335images) using a separate "normal" instance of Android Studio. "Normal" means a
336non-Canary build of Studio that you would use for regular app development -- the
337important part being that it points to the Android SDK where your downloaded
338emulator images reside. You will need to open a project to get the Tools menu --
339do NOT open the AndroidX project in the "normal" instance of Android Studio;
340instead, open a normal app or create a blank project using the app wizard.
341
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +0000342NOTE You can reuse the emulator and system images from a "normal" installation
343of Android Studio by linking the `emulator` and `system_images` directories to a
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000344standard Android SDK path and restarting Android Studio. **This is set up
345automatically by `studiow` on Google-managed devices with a standard Android SDK
346path.** In other cases, it may be set up manually with something like: `cd
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +0000347prebuilts/fullsdk-darwin ln -s ~/Library/Android/sdk/emulator emulator ln -s
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000348~/Library/Android/sdk/system-images system-images` (substituting `fullsdk-linux`
349and your local SDK path as appropriate)
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +0000350
AndroidX Core Team9b7c30e2024-12-05 09:56:33 -0800351## Debugging tests
352
353### Using custom platform SDK sources {#sources}
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000354
355The platform SDK sources that are checked into the development branch may not
356match up with the build of Android present on the emulator or your physical
357device. As a result, the line numbers reported by the debugger may not match up
358the actual code being run.
359
360If you have a copy of the sources for the build against which you are debugging,
361you can manually specify your platform SDK source path:
362
3631. Click on a module (e.g. `appcompat`) in the `Project` view
3641. Press `Ctrl-Shift-A` and type "Module Settings", then run the action
3651. In the `Project Structure` dialog, navigate to `SDKs > Android API 29
366 Platform > Sourcepath`
3671. 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
372NOTE The `Project Structure` dialog reachable via `File > Project Structure` is
373**not** the same as the `Project Structure` dialog that will allow you to
374specify the SDK source path. You must use the "Module Settings" action as
375directed above.
376
AndroidX Core Team9b7c30e2024-12-05 09:56:33 -0800377### Accessing FTL outputs
378
379When we run tests on Firebase Test Lab devices, we transfer the results and
380logcat output back to Android's test result infrastructure; however, FTL also
381captures screen recordings of the entire test run.
382
383To access these videos from the Android Test Investigate page for a failed test
384run:
385
386- For the failing test, go to `Artifacts tab` in the Android Test Investigate
387 page
388- Disable `Hide empty folders` (if enabled) by clicking on it
389- Under `Run artifacts`, click on "i" icon next to the test module to open the
390 Information tab
391- In the Information tab to the right, click on the link next to the `logs`
392 property
393
394The full logcat output and screen recording are available from the `Devices` tab
395by clicking on the test device under `Device details` and using the `Logs` and
396`Video` tabs, respectively.
397
398Per-test logcat output and videos are available from the `Test cases` tab.
399
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000400## Running unit and integration tests {#running}
401
402From Android Studio, right-click can be used to run most test targets, including
403source files, classes within a file, or individual test methods but **not**
404entire modules. To run a supported test target, right-click on the test target
405and then click `Run <name of test target>`.
406
407To run tests for an entire module such as `appcompat`, use `Run -> Edit
408configurations...` and use the `+` button to create a new `Android Instrumented
409Tests` configuration. Specify the module to be tested, give it a reasonable name
410(not "All Tests") and click `OK`, then use the `Run` menu to run the
411configuration.
412
413![alt_text](onboarding_images/image2.png "screenshot of run menu")
414
415NOTE If you receive the error `JUnit version 3.8 or later expected` this means
416that Android Studio generated an Android JUnit configuration when you actually
417needed an Android Instrumented Tests configuration. Open the `Run -> Edit
418configurations...` dialog and delete the configuration from Android JUnit, then
419manually add a configuration in Android Instrumented Tests.
420
421### From the command line {#running-from-shell}
422
423Following a successful build, tests may be run against a particular AndroidX
424module using `gradlew`.
425
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000426To run all unit or integration tests in a specific project, run the following
427from `framework/support`:
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000428
429```shell
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000430# Run instrumentation tests on a connected device
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800431./gradlew <project-name>:connectedAndroidTest --info
432
433# Run instrumentation tests in Firebase Test Lab (remote)
434./gradlew <project-name>:ftlnexus4api21
435./gradlew <project-name>:ftlpixel2api26
436./gradlew <project-name>:ftlpixel2api28
437./gradlew <project-name>:ftlpixel2api30
438./gradlew <project-name>:ftlpixel2api33
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000439
440# Run local unit tests
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800441./gradlew <project-name>:test
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000442```
443
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800444substituting the Gradle project name (ex. `:core:core`).
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000445
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800446To run a specific instrumentation test in a given project, run
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000447
448```shell
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800449# Run instrumentation tests on a connected device
450./gradlew <project-name>:connectedAndroidTest --info \
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000451 -Pandroid.testInstrumentationRunnerArguments.class=<fully-qualified-class>[\#testName]
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800452
453# Run instrumentation tests on in Firebase Test Lab (remote)
454./gradlew <project-name>:ftlpixel2api30 --className=<fully-qualified-class>
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000455```
456
457substituting the Gradle project name (ex. `viewpager`) and fully-qualified class
458name (ex. `androidx.viewpager.widget.ViewPagerTest`) of your test file,
459optionally followed by `\#testName` if you want to execute a single test in that
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800460file
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000461
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800462If you want to run a specific unit test, you can do it using
AndroidX Core Teama200cb82023-03-28 15:23:28 -0700463[`--tests` filtering](https://docs.gradle.org/current/userguide/java_testing.html#test_filtering):
464
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800465```shell
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800466# Run a test for an Android library on a connected device
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800467./gradlew <project-name>:test --tests androidx.core.view.DisplayCompatTest
468
469# Run a test for a JVM library
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800470./gradlew <project-name>:testDebugUnitTest --tests
AndroidX Core Teama200cb82023-03-28 15:23:28 -0700471androidx.core.view.DisplayCompatTest
472```
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000473
474## Test apps {#testapps}
475
476Library developers are strongly encouraged to write test apps that exercise
477their library's public API surface. Test apps serve multiple purposes:
478
479* Integration testing and validation of API testability, when paired with
480 tests
481* Validation of API usability and developer experience, when paired with a use
482 case or critical user journey
483* Sample documentation, when embedded into API reference docs using the
Ian Baker186108e2023-11-20 06:54:36 -0800484 [`@sample` and `@Sampled` annotations](/docs/api_guidelines/index.md#sample-usage)
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000485
486### Legacy test apps {#testapps-legacy}
487
488We have a set of legacy sample Android applications in projects suffixed with
489`-demos`. These applications do not have tests and should not be used as test
490apps for new APIs, but they may be useful for manual regression testing.
491
4921. Click `Run/Debug Configuration` on the top of the window.
4931. Select the app you want to run.
4941. Click 'Run' button.
495
496![alt_text](onboarding_images/image3.png "screenshot of Run/Debug menu")
497
498## Benchmarking {#benchmarking}
499
500AndroidX supports benchmarking - locally with Studio/Gradle, and continuously in
501post-submit. For more information on how to create and run benchmarks, see
Ian Baker186108e2023-11-20 06:54:36 -0800502[Benchmarking](/docs/benchmarking.md).