blob: 8050e0a5f8ee586580967b357d7d9a9540d55080 [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
10## Adding tests {#adding}
11
12For an example of how to set up simple unit and integration tests in a new
13module, see
14[aosp/1189799](https://android-review.googlesource.com/c/platform/frameworks/support/+/1189799).
15For an example of how to set up Espresso-powered integration tests, see the
16`preference` library's
AndroidX Core Team408c27b2020-12-15 15:57:00 +000017[`build.gradle`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:preference/preference/build.gradle)
AndroidX Core Team2e416b22020-12-03 22:58:07 +000018and
AndroidX Core Team408c27b2020-12-15 15:57:00 +000019[`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 +000020files.
21
22The currently allowed test runners for on-device tests are
23[`AndroidJUnitRunner`](https://developer.android.com/training/testing/junit-runner)
24and
25[`Parameterized`](https://junit.org/junit4/javadoc/4.12/org/junit/runners/Parameterized.html).
26
AndroidX Core Teamb5ba61d2021-06-08 09:20:36 -070027NOTE All package/class/method combinations must be unique. Multiple copies of
28the same class/method can be included e.g. under different directories, but must
29be distinguishable by their packages.
30
AndroidX Core Team03b4da32021-03-10 23:20:41 +000031NOTE For best practices on writing libraries in a way that makes it easy for end
32users -- and library developers -- to write tests, see the
AndroidX Core Team5fa61982023-01-13 10:43:41 -050033[Testability](/company/teams/androidx/testability.md) guide.
AndroidX Core Team03b4da32021-03-10 23:20:41 +000034
AndroidX Core Team5330eef2023-02-21 16:07:59 -050035### Adding a JVM based screenshot test
36
37For UI heavy libraries, it might make sense to add screenshot tests to verify
38that everything still renders as expected. For that you need to write the test
39([example](https://r.android.com/2428035)) and add new goldens
40([example](https://r.android.com/2428721)). You can run these tests just like
41any other JVM test using `test` Gradle task.
42
AndroidX Core Team21ccf652022-04-01 14:53:07 +000043### What gets tested, and when {#affected-module-detector}
AndroidX Core Team2e416b22020-12-03 22:58:07 +000044
AndroidX Core Team3da62632022-10-03 11:29:25 -070045With over 45000 tests executed on every CI run, it is necessary for us to run
46only a subset of our instrumentation tests in presubmit. We use the
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +000047[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 -070048to determine what projects have changed since the last merge. In turn, we only
49generate apks and test configurations for those changed modules and their
50dependencies.
AndroidX Core Team2e416b22020-12-03 22:58:07 +000051
52When changes are made that can't be associated with a module, are in the root of
53the checkout, or are within `buildSrc`, then all host tests and all device tests
54annotated with `@SmallTest` or `@MediumTest` will be run for all modules.
55
56Presubmit tests represent only a subset of the devices on which our tests run.
57The remaining devices are tested only in postsubmit. In postsubmit, all host and
58device tests are run for all modules.
59
AndroidX Core Team21ccf652022-04-01 14:53:07 +000060### Test annotations {#annotations}
AndroidX Core Team2e416b22020-12-03 22:58:07 +000061
AndroidX Core Team21ccf652022-04-01 14:53:07 +000062#### Test size and runners {#test-size}
AndroidX Core Team2e416b22020-12-03 22:58:07 +000063
64All device tests *should* be given a size annotation, which is one of:
65
66* [`@SmallTest`](https://developer.android.com/reference/androidx/test/filters/SmallTest)
67* [`@MediumTest`](https://developer.android.com/reference/androidx/test/filters/MediumTest)
68* [`@LargeTest`](https://developer.android.com/reference/androidx/test/filters/LargeTest)
69
alanv37fed3a22021-09-17 07:46:47 -070070If 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 +000071`@LargeTest` by default. Host tests do not need to be annotated with their size,
72as all host tests are run regardless of size.
73
74This annotation can occur at either the class level or individual test level.
AndroidX Core Team2e416b22020-12-03 22:58:07 +000075
AndroidX Core Teamb5ba61d2021-06-08 09:20:36 -070076Annotation | Max duration
77------------- | ------------
78`@SmallTest` | 200ms
79`@MediumTest` | 1000ms
80`@LargeTest` | 100000ms
AndroidX Core Team2e416b22020-12-03 22:58:07 +000081
AndroidX Core Team21ccf652022-04-01 14:53:07 +000082#### Disabling tests {#disabling-tests}
AndroidX Core Team2e416b22020-12-03 22:58:07 +000083
84To disable a device-side test in presubmit testing only -- but still have it run
85in postsubmit -- use the
86[`@FlakyTest`](https://developer.android.com/reference/androidx/test/filters/FlakyTest)
87annotation. There is currently no support for presubmit-only disabling of
88host-side tests.
89
90If you need to stop a host- or device-side test from running entirely, use
91JUnit's [`@Ignore`](http://junit.sourceforge.net/javadoc/org/junit/Ignore.html)
92annotation. Do *not* use Android's `@Suppress` annotation, which only works with
93Android test runners and will *not* work for host-side tests.
94
AndroidX Core Team21ccf652022-04-01 14:53:07 +000095#### Filtering devices {#filtering-devices}
AndroidX Core Team2e416b22020-12-03 22:58:07 +000096
97To restrict a test to a range of SDKs, use
98[`@SdkSuppress`](https://developer.android.com/reference/androidx/test/filters/SdkSuppress)
99which allows specifying a range with `minSdkVersion` and `maxSdkVersion`. This
100annotation 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
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000107// Target pre-release SDK T only
108@SdkSuppress(minSdkVersion = Build.VERSION_CODES.TIRAMISU, codeName = "Tiramisu")
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000109```
110
111You may also gate portions of test implementation code using `SDK_INT` or
112[`BuildCompat.isAtLeast`](https://developer.android.com/reference/androidx/core/os/BuildCompat)
AndroidX Core Team25bc9332021-08-10 11:11:26 -0700113methods. s To restrict to only physical devices, use
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000114[`@RequiresDevice`](https://developer.android.com/reference/androidx/test/filters/RequiresDevice).
115
AndroidX Core Team5c914c42021-02-08 17:22:57 +0000116NOTE [Cuttlefish](https://source.android.com/setup/create/cuttlefish) is not
117affected by this annotation, only e.g. Studio emulators. If Cuttlefish is
118displaying behavior that differs from a physical device, they are considering
119that a bug in Cuttlefish, so please file those bugs instead of only looking for
120a workaround.
121
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000122### Animations in tests {#animations}
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000123
124Animations are disabled for tests by default. This helps avoid flakes due to
125timing and also makes tests faster.
126
127In rare cases, like testing the animations themselves, you may want to enable
128animations for a particular test or test class. For those cases, you can use the
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000129[`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 +0000130
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000131### Robolectric {#robolectric}
alanvf21d4ab2021-08-18 07:43:40 -0700132
133Robolectric tests are supported in AndroidX; however, if you targeting a
134pre-release version of the Android SDK then you may see an error like
135
136```
alanv9102ecc2022-08-26 07:46:41 -0700137java.lang.IllegalArgumentException: Package targetSdkVersion=31 > maxSdkVersion=30
alanvf21d4ab2021-08-18 07:43:40 -0700138at org.robolectric.plugins.DefaultSdkPicker.configuredSdks(DefaultSdkPicker.java:118)
139at org.robolectric.plugins.DefaultSdkPicker.selectSdks(DefaultSdkPicker.java:69)
140```
141
142You can force Robolectric to run using an earlier version of the platform SDK by
143creating a `<project>/src/test/resources/robolectric.properties` file with the
144following contents:
145
146```
alanv9102ecc2022-08-26 07:46:41 -0700147# Robolectric currently doesn't support API 31, so we have to explicitly specify 30 as the target
alanvf21d4ab2021-08-18 07:43:40 -0700148# sdk for now. Remove when no longer necessary.
alanv9102ecc2022-08-26 07:46:41 -0700149sdk=30
alanvf21d4ab2021-08-18 07:43:40 -0700150```
151
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000152## Using the emulator {#emulator}
153
154You can use the emulator or a real device to run tests. If you wish to use the
155emulator, you will need to access the AVD Manager (and your downloaded emulator
156images) using a separate "normal" instance of Android Studio. "Normal" means a
157non-Canary build of Studio that you would use for regular app development -- the
158important part being that it points to the Android SDK where your downloaded
159emulator images reside. You will need to open a project to get the Tools menu --
160do NOT open the AndroidX project in the "normal" instance of Android Studio;
161instead, open a normal app or create a blank project using the app wizard.
162
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +0000163NOTE You can reuse the emulator and system images from a "normal" installation
164of Android Studio by linking the `emulator` and `system_images` directories to a
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000165standard Android SDK path and restarting Android Studio. **This is set up
166automatically by `studiow` on Google-managed devices with a standard Android SDK
167path.** In other cases, it may be set up manually with something like: `cd
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +0000168prebuilts/fullsdk-darwin ln -s ~/Library/Android/sdk/emulator emulator ln -s
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000169~/Library/Android/sdk/system-images system-images` (substituting `fullsdk-linux`
170and your local SDK path as appropriate)
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +0000171
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000172## Debugging with platform SDK sources {#sources}
173
174The platform SDK sources that are checked into the development branch may not
175match up with the build of Android present on the emulator or your physical
176device. As a result, the line numbers reported by the debugger may not match up
177the actual code being run.
178
179If you have a copy of the sources for the build against which you are debugging,
180you can manually specify your platform SDK source path:
181
1821. Click on a module (e.g. `appcompat`) in the `Project` view
1831. Press `Ctrl-Shift-A` and type "Module Settings", then run the action
1841. In the `Project Structure` dialog, navigate to `SDKs > Android API 29
185 Platform > Sourcepath`
1861. Use the `-` button to remove any paths that are present, then use the `+`
187 button to add the desired source path, ex. `<android checkout
188 root>/frameworks/base` if you are debugging against a locally-built system
189 image
190
191NOTE The `Project Structure` dialog reachable via `File > Project Structure` is
192**not** the same as the `Project Structure` dialog that will allow you to
193specify the SDK source path. You must use the "Module Settings" action as
194directed above.
195
196## Running unit and integration tests {#running}
197
198From Android Studio, right-click can be used to run most test targets, including
199source files, classes within a file, or individual test methods but **not**
200entire modules. To run a supported test target, right-click on the test target
201and then click `Run <name of test target>`.
202
203To run tests for an entire module such as `appcompat`, use `Run -> Edit
204configurations...` and use the `+` button to create a new `Android Instrumented
205Tests` configuration. Specify the module to be tested, give it a reasonable name
206(not "All Tests") and click `OK`, then use the `Run` menu to run the
207configuration.
208
209![alt_text](onboarding_images/image2.png "screenshot of run menu")
210
211NOTE If you receive the error `JUnit version 3.8 or later expected` this means
212that Android Studio generated an Android JUnit configuration when you actually
213needed an Android Instrumented Tests configuration. Open the `Run -> Edit
214configurations...` dialog and delete the configuration from Android JUnit, then
215manually add a configuration in Android Instrumented Tests.
216
217### From the command line {#running-from-shell}
218
219Following a successful build, tests may be run against a particular AndroidX
220module using `gradlew`.
221
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000222To run all unit or integration tests in a specific project, run the following
223from `framework/support`:
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000224
225```shell
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000226# Run instrumentation tests on a connected device
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800227./gradlew <project-name>:connectedAndroidTest --info
228
229# Run instrumentation tests in Firebase Test Lab (remote)
230./gradlew <project-name>:ftlnexus4api21
231./gradlew <project-name>:ftlpixel2api26
232./gradlew <project-name>:ftlpixel2api28
233./gradlew <project-name>:ftlpixel2api30
234./gradlew <project-name>:ftlpixel2api33
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000235
236# Run local unit tests
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800237./gradlew <project-name>:test
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000238```
239
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800240substituting the Gradle project name (ex. `:core:core`).
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000241
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800242To run a specific instrumentation test in a given project, run
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000243
244```shell
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800245# Run instrumentation tests on a connected device
246./gradlew <project-name>:connectedAndroidTest --info \
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000247 -Pandroid.testInstrumentationRunnerArguments.class=<fully-qualified-class>[\#testName]
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800248
249# Run instrumentation tests on in Firebase Test Lab (remote)
250./gradlew <project-name>:ftlpixel2api30 --className=<fully-qualified-class>
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000251```
252
253substituting the Gradle project name (ex. `viewpager`) and fully-qualified class
254name (ex. `androidx.viewpager.widget.ViewPagerTest`) of your test file,
255optionally followed by `\#testName` if you want to execute a single test in that
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800256file
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000257
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800258If you want to run a specific unit test, you can do it using
AndroidX Core Teama200cb82023-03-28 15:23:28 -0700259[`--tests` filtering](https://docs.gradle.org/current/userguide/java_testing.html#test_filtering):
260
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800261```shell
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800262# Run a test for an Android library on a connected device
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800263./gradlew <project-name>:test --tests androidx.core.view.DisplayCompatTest
264
265# Run a test for a JVM library
AndroidX Core Team8c9a1c02023-03-08 14:16:36 -0800266./gradlew <project-name>:testDebugUnitTest --tests
AndroidX Core Teama200cb82023-03-28 15:23:28 -0700267androidx.core.view.DisplayCompatTest
268```
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000269
270## Test apps {#testapps}
271
272Library developers are strongly encouraged to write test apps that exercise
273their library's public API surface. Test apps serve multiple purposes:
274
275* Integration testing and validation of API testability, when paired with
276 tests
277* Validation of API usability and developer experience, when paired with a use
278 case or critical user journey
279* Sample documentation, when embedded into API reference docs using the
AndroidX Core Team5fa61982023-01-13 10:43:41 -0500280 [`@sample` and `@Sampled` annotations](/company/teams/androidx/api_guidelines/index.md#sample-usage)
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000281
282### Legacy test apps {#testapps-legacy}
283
284We have a set of legacy sample Android applications in projects suffixed with
285`-demos`. These applications do not have tests and should not be used as test
286apps for new APIs, but they may be useful for manual regression testing.
287
2881. Click `Run/Debug Configuration` on the top of the window.
2891. Select the app you want to run.
2901. Click 'Run' button.
291
292![alt_text](onboarding_images/image3.png "screenshot of Run/Debug menu")
293
294## Benchmarking {#benchmarking}
295
296AndroidX supports benchmarking - locally with Studio/Gradle, and continuously in
297post-submit. For more information on how to create and run benchmarks, see
AndroidX Core Team5fa61982023-01-13 10:43:41 -0500298[Benchmarking](/company/teams/androidx/benchmarking.md).