blob: 2829821bac9550b5c171cbb4ab5c04d7cb734ce1 [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
33[Testability](testability.md) guide.
34
AndroidX Core Team21ccf652022-04-01 14:53:07 +000035### What gets tested, and when {#affected-module-detector}
AndroidX Core Team2e416b22020-12-03 22:58:07 +000036
37We use the
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +000038[AffectedModuleDetector](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:buildSrc/private/src/main/kotlin/androidx/build/dependencyTracker/AffectedModuleDetector.kt)
AndroidX Core Team2e416b22020-12-03 22:58:07 +000039to determine what projects have changed since the last merge.
40
41In presubmit, "affected" modules will run all host and device tests regardless
alanv37fed3a22021-09-17 07:46:47 -070042of size. Modules that *depend* on affected modules will run all host tests, but
AndroidX Core Team2e416b22020-12-03 22:58:07 +000043will only run device tests annotated with `@SmallTest` or `@MediumTest`.
44
45When changes are made that can't be associated with a module, are in the root of
46the checkout, or are within `buildSrc`, then all host tests and all device tests
47annotated with `@SmallTest` or `@MediumTest` will be run for all modules.
48
49Presubmit tests represent only a subset of the devices on which our tests run.
50The remaining devices are tested only in postsubmit. In postsubmit, all host and
51device tests are run for all modules.
52
AndroidX Core Team21ccf652022-04-01 14:53:07 +000053### Test annotations {#annotations}
AndroidX Core Team2e416b22020-12-03 22:58:07 +000054
AndroidX Core Team21ccf652022-04-01 14:53:07 +000055#### Test size and runners {#test-size}
AndroidX Core Team2e416b22020-12-03 22:58:07 +000056
57All 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
alanv37fed3a22021-09-17 07:46:47 -070063If 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 +000064`@LargeTest` by default. Host tests do not need to be annotated with their size,
65as all host tests are run regardless of size.
66
67This annotation can occur at either the class level or individual test level.
AndroidX Core Team2e416b22020-12-03 22:58:07 +000068
AndroidX Core Teamb5ba61d2021-06-08 09:20:36 -070069Annotation | Max duration
70------------- | ------------
71`@SmallTest` | 200ms
72`@MediumTest` | 1000ms
73`@LargeTest` | 100000ms
AndroidX Core Team2e416b22020-12-03 22:58:07 +000074
AndroidX Core Team21ccf652022-04-01 14:53:07 +000075#### Disabling tests {#disabling-tests}
AndroidX Core Team2e416b22020-12-03 22:58:07 +000076
77To disable a device-side test in presubmit testing only -- but still have it run
78in postsubmit -- use the
79[`@FlakyTest`](https://developer.android.com/reference/androidx/test/filters/FlakyTest)
80annotation. There is currently no support for presubmit-only disabling of
81host-side tests.
82
83If you need to stop a host- or device-side test from running entirely, use
84JUnit's [`@Ignore`](http://junit.sourceforge.net/javadoc/org/junit/Ignore.html)
85annotation. Do *not* use Android's `@Suppress` annotation, which only works with
86Android test runners and will *not* work for host-side tests.
87
AndroidX Core Team21ccf652022-04-01 14:53:07 +000088#### Filtering devices {#filtering-devices}
AndroidX Core Team2e416b22020-12-03 22:58:07 +000089
90To restrict a test to a range of SDKs, use
91[`@SdkSuppress`](https://developer.android.com/reference/androidx/test/filters/SdkSuppress)
92which allows specifying a range with `minSdkVersion` and `maxSdkVersion`. This
93annotation 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 Team21ccf652022-04-01 14:53:07 +0000100// Target pre-release SDK T only
101@SdkSuppress(minSdkVersion = Build.VERSION_CODES.TIRAMISU, codeName = "Tiramisu")
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000102```
103
104You 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 Team25bc9332021-08-10 11:11:26 -0700106methods. s To restrict to only physical devices, use
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000107[`@RequiresDevice`](https://developer.android.com/reference/androidx/test/filters/RequiresDevice).
108
AndroidX Core Team5c914c42021-02-08 17:22:57 +0000109NOTE [Cuttlefish](https://source.android.com/setup/create/cuttlefish) is not
110affected by this annotation, only e.g. Studio emulators. If Cuttlefish is
111displaying behavior that differs from a physical device, they are considering
112that a bug in Cuttlefish, so please file those bugs instead of only looking for
113a workaround.
114
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000115### Animations in tests {#animations}
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000116
117Animations are disabled for tests by default. This helps avoid flakes due to
118timing and also makes tests faster.
119
120In rare cases, like testing the animations themselves, you may want to enable
121animations for a particular test or test class. For those cases, you can use the
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000122[`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 +0000123
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000124### Robolectric {#robolectric}
alanvf21d4ab2021-08-18 07:43:40 -0700125
126Robolectric tests are supported in AndroidX; however, if you targeting a
127pre-release version of the Android SDK then you may see an error like
128
129```
130java.lang.IllegalArgumentException: Package targetSdkVersion=31 > maxSdkVersion=30
131at org.robolectric.plugins.DefaultSdkPicker.configuredSdks(DefaultSdkPicker.java:118)
132at org.robolectric.plugins.DefaultSdkPicker.selectSdks(DefaultSdkPicker.java:69)
133```
134
135You can force Robolectric to run using an earlier version of the platform SDK by
136creating a `<project>/src/test/resources/robolectric.properties` file with the
137following contents:
138
139```
alanvdf3e8b92021-11-30 13:13:05 -0800140# Robolectric currently doesn't support API 31, so we have to explicitly specify 30 as the target
alanvf21d4ab2021-08-18 07:43:40 -0700141# sdk for now. Remove when no longer necessary.
alanvdf3e8b92021-11-30 13:13:05 -0800142sdk=30
alanvf21d4ab2021-08-18 07:43:40 -0700143```
144
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000145## Using the emulator {#emulator}
146
147You can use the emulator or a real device to run tests. If you wish to use the
148emulator, you will need to access the AVD Manager (and your downloaded emulator
149images) using a separate "normal" instance of Android Studio. "Normal" means a
150non-Canary build of Studio that you would use for regular app development -- the
151important part being that it points to the Android SDK where your downloaded
152emulator images reside. You will need to open a project to get the Tools menu --
153do NOT open the AndroidX project in the "normal" instance of Android Studio;
154instead, open a normal app or create a blank project using the app wizard.
155
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +0000156NOTE You can reuse the emulator and system images from a "normal" installation
157of Android Studio by linking the `emulator` and `system_images` directories to a
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000158standard Android SDK path and restarting Android Studio. **This is set up
159automatically by `studiow` on Google-managed devices with a standard Android SDK
160path.** In other cases, it may be set up manually with something like: `cd
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +0000161prebuilts/fullsdk-darwin ln -s ~/Library/Android/sdk/emulator emulator ln -s
AndroidX Core Team21ccf652022-04-01 14:53:07 +0000162~/Library/Android/sdk/system-images system-images` (substituting `fullsdk-linux`
163and your local SDK path as appropriate)
AndroidX Core Team4cc85fa2021-11-23 15:58:34 +0000164
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000165## Debugging with platform SDK sources {#sources}
166
167The platform SDK sources that are checked into the development branch may not
168match up with the build of Android present on the emulator or your physical
169device. As a result, the line numbers reported by the debugger may not match up
170the actual code being run.
171
172If you have a copy of the sources for the build against which you are debugging,
173you can manually specify your platform SDK source path:
174
1751. Click on a module (e.g. `appcompat`) in the `Project` view
1761. Press `Ctrl-Shift-A` and type "Module Settings", then run the action
1771. In the `Project Structure` dialog, navigate to `SDKs > Android API 29
178 Platform > Sourcepath`
1791. 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
184NOTE The `Project Structure` dialog reachable via `File > Project Structure` is
185**not** the same as the `Project Structure` dialog that will allow you to
186specify the SDK source path. You must use the "Module Settings" action as
187directed above.
188
189## Running unit and integration tests {#running}
190
191From Android Studio, right-click can be used to run most test targets, including
192source files, classes within a file, or individual test methods but **not**
193entire modules. To run a supported test target, right-click on the test target
194and then click `Run <name of test target>`.
195
196To run tests for an entire module such as `appcompat`, use `Run -> Edit
197configurations...` and use the `+` button to create a new `Android Instrumented
198Tests` 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
200configuration.
201
202![alt_text](onboarding_images/image2.png "screenshot of run menu")
203
204NOTE If you receive the error `JUnit version 3.8 or later expected` this means
205that Android Studio generated an Android JUnit configuration when you actually
206needed an Android Instrumented Tests configuration. Open the `Run -> Edit
207configurations...` dialog and delete the configuration from Android JUnit, then
208manually add a configuration in Android Instrumented Tests.
209
210### From the command line {#running-from-shell}
211
212Following a successful build, tests may be run against a particular AndroidX
213module using `gradlew`.
214
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000215To run all unit or integration tests in a specific project, run the following
216from `framework/support`:
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000217
218```shell
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000219# 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 Team2e416b22020-12-03 22:58:07 +0000224```
225
226substituting the Gradle project name (ex. `core`).
227
228To run all integration tests in the specific project and test class you're
229working on, run
230
231```shell
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000232./gradlew <project-name>:connectedAndroidTest --info --daemon \
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000233 -Pandroid.testInstrumentationRunnerArguments.class=<fully-qualified-class>[\#testName]
234```
235
236substituting the Gradle project name (ex. `viewpager`) and fully-qualified class
237name (ex. `androidx.viewpager.widget.ViewPagerTest`) of your test file,
238optionally followed by `\#testName` if you want to execute a single test in that
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000239file. Substitute `test` for `connectedAndroidTest` to run local unit tests.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000240
241If you see some weird compilation errors such as below, run `./gradlew clean`
242first:
243
244```
245Unknown source file : UNEXPECTED TOP-LEVEL EXCEPTION:
246Unknown source file : com.android.dex.DexException: Multiple dex files define Landroid/content/pm/ParceledListSlice$1;
247```
248
249## Test apps {#testapps}
250
251Library developers are strongly encouraged to write test apps that exercise
252their 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
263We 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
265apps for new APIs, but they may be useful for manual regression testing.
266
2671. Click `Run/Debug Configuration` on the top of the window.
2681. Select the app you want to run.
2691. Click 'Run' button.
270
271![alt_text](onboarding_images/image3.png "screenshot of Run/Debug menu")
272
273## Benchmarking {#benchmarking}
274
275AndroidX supports benchmarking - locally with Studio/Gradle, and continuously in
276post-submit. For more information on how to create and run benchmarks, see
277[Benchmarking](benchmarking.md).