blob: d6ad752e2af731b7fab14f84033353e1415c1eae [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 Team2e416b22020-12-03 22:58:07 +000035### What gets tested, and when
36
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
53### Test annotations
54
AndroidX Core Teamb5ba61d2021-06-08 09:20:36 -070055#### Test size and runners
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
75#### Disabling tests
76
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
88#### Filtering devices
89
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
100// Target pre-release SDK R only
101@SdkSuppress(minSdkVersion = Build.VERSION_CODES.R, isCodeName = "R")
102```
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 Team2e416b22020-12-03 22:58:07 +0000115### Animations in tests
116
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
alanvf21d4ab2021-08-18 07:43:40 -0700124### Robolectric
125
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
158standard Android SDK path and restarting Android Studio: `cd
159prebuilts/fullsdk-darwin ln -s ~/Library/Android/sdk/emulator emulator ln -s
160~/Library/Android/sdk/system-images system-images`
161
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000162## Debugging with platform SDK sources {#sources}
163
164The platform SDK sources that are checked into the development branch may not
165match up with the build of Android present on the emulator or your physical
166device. As a result, the line numbers reported by the debugger may not match up
167the actual code being run.
168
169If you have a copy of the sources for the build against which you are debugging,
170you can manually specify your platform SDK source path:
171
1721. Click on a module (e.g. `appcompat`) in the `Project` view
1731. Press `Ctrl-Shift-A` and type "Module Settings", then run the action
1741. In the `Project Structure` dialog, navigate to `SDKs > Android API 29
175 Platform > Sourcepath`
1761. Use the `-` button to remove any paths that are present, then use the `+`
177 button to add the desired source path, ex. `<android checkout
178 root>/frameworks/base` if you are debugging against a locally-built system
179 image
180
181NOTE The `Project Structure` dialog reachable via `File > Project Structure` is
182**not** the same as the `Project Structure` dialog that will allow you to
183specify the SDK source path. You must use the "Module Settings" action as
184directed above.
185
186## Running unit and integration tests {#running}
187
188From Android Studio, right-click can be used to run most test targets, including
189source files, classes within a file, or individual test methods but **not**
190entire modules. To run a supported test target, right-click on the test target
191and then click `Run <name of test target>`.
192
193To run tests for an entire module such as `appcompat`, use `Run -> Edit
194configurations...` and use the `+` button to create a new `Android Instrumented
195Tests` configuration. Specify the module to be tested, give it a reasonable name
196(not "All Tests") and click `OK`, then use the `Run` menu to run the
197configuration.
198
199![alt_text](onboarding_images/image2.png "screenshot of run menu")
200
201NOTE If you receive the error `JUnit version 3.8 or later expected` this means
202that Android Studio generated an Android JUnit configuration when you actually
203needed an Android Instrumented Tests configuration. Open the `Run -> Edit
204configurations...` dialog and delete the configuration from Android JUnit, then
205manually add a configuration in Android Instrumented Tests.
206
207### From the command line {#running-from-shell}
208
209Following a successful build, tests may be run against a particular AndroidX
210module using `gradlew`.
211
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000212To run all unit or integration tests in a specific project, run the following
213from `framework/support`:
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000214
215```shell
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000216# Run instrumentation tests on a connected device
217./gradlew <project-name>:connectedAndroidTest --info --daemon
218
219# Run local unit tests
220./gradlew <project-name>:test --info --daemon
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000221```
222
223substituting the Gradle project name (ex. `core`).
224
225To run all integration tests in the specific project and test class you're
226working on, run
227
228```shell
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000229./gradlew <project-name>:connectedAndroidTest --info --daemon \
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000230 -Pandroid.testInstrumentationRunnerArguments.class=<fully-qualified-class>[\#testName]
231```
232
233substituting the Gradle project name (ex. `viewpager`) and fully-qualified class
234name (ex. `androidx.viewpager.widget.ViewPagerTest`) of your test file,
235optionally followed by `\#testName` if you want to execute a single test in that
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000236file. Substitute `test` for `connectedAndroidTest` to run local unit tests.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000237
238If you see some weird compilation errors such as below, run `./gradlew clean`
239first:
240
241```
242Unknown source file : UNEXPECTED TOP-LEVEL EXCEPTION:
243Unknown source file : com.android.dex.DexException: Multiple dex files define Landroid/content/pm/ParceledListSlice$1;
244```
245
246## Test apps {#testapps}
247
248Library developers are strongly encouraged to write test apps that exercise
249their library's public API surface. Test apps serve multiple purposes:
250
251* Integration testing and validation of API testability, when paired with
252 tests
253* Validation of API usability and developer experience, when paired with a use
254 case or critical user journey
255* Sample documentation, when embedded into API reference docs using the
256 [`@sample` and `@Sampled` annotations](api_guidelines.md#sample-usage)
257
258### Legacy test apps {#testapps-legacy}
259
260We have a set of legacy sample Android applications in projects suffixed with
261`-demos`. These applications do not have tests and should not be used as test
262apps for new APIs, but they may be useful for manual regression testing.
263
2641. Click `Run/Debug Configuration` on the top of the window.
2651. Select the app you want to run.
2661. Click 'Run' button.
267
268![alt_text](onboarding_images/image3.png "screenshot of Run/Debug menu")
269
270## Benchmarking {#benchmarking}
271
272AndroidX supports benchmarking - locally with Studio/Gradle, and continuously in
273post-submit. For more information on how to create and run benchmarks, see
274[Benchmarking](benchmarking.md).