blob: f7570021d7e58bab32e119e9523fe1bbe9875c80 [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
27### What gets tested, and when
28
29We use the
AndroidX Core Team408c27b2020-12-15 15:57:00 +000030[AffectedModuleDetector](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:buildSrc/src/main/kotlin/androidx/build/dependencyTracker/AffectedModuleDetector.kt)
AndroidX Core Team2e416b22020-12-03 22:58:07 +000031to determine what projects have changed since the last merge.
32
33In presubmit, "affected" modules will run all host and device tests regardless
34of size. Modules that _depend_ on affected modules will run all host tests, but
35will only run device tests annotated with `@SmallTest` or `@MediumTest`.
36
37When changes are made that can't be associated with a module, are in the root of
38the checkout, or are within `buildSrc`, then all host tests and all device tests
39annotated with `@SmallTest` or `@MediumTest` will be run for all modules.
40
41Presubmit tests represent only a subset of the devices on which our tests run.
42The remaining devices are tested only in postsubmit. In postsubmit, all host and
43device tests are run for all modules.
44
45### Test annotations
46
47#### Test size
48
49All device tests *should* be given a size annotation, which is one of:
50
51* [`@SmallTest`](https://developer.android.com/reference/androidx/test/filters/SmallTest)
52* [`@MediumTest`](https://developer.android.com/reference/androidx/test/filters/MediumTest)
53* [`@LargeTest`](https://developer.android.com/reference/androidx/test/filters/LargeTest)
54
55If a device test is _not_ annotated with its size, it will be considered a
56`@LargeTest` by default. Host tests do not need to be annotated with their size,
57as all host tests are run regardless of size.
58
59This annotation can occur at either the class level or individual test level.
60After API level 27, timeouts are enforced based on this annotation.
61
62Annotation | Max duration | Timeout after
63------------- | ------------ | -------------
64`@SmallTest` | 200ms | 300ms
65`@MediumTest` | 1000ms | 1500ms
66`@LargeTest` | 100000ms | 120000ms
67
68Small tests should be less than 200ms, and the timeout is set to 300ms. Medium
69tests should be less than 1000ms, and the timeout is set to 1500ms. Large tests
70have a timeout of 120000ms, which should cover any remaining tests.
71
72The exception to this rule is when using a runner other than
73[`AndroidJUnitRunner`](https://developer.android.com/training/testing/junit-runner).
74Since these runners do not enforce timeouts, tests that use them must not use a
75size annotation. They will run whenever large tests would run.
76
77Currently the only allowed alternative is the
78[`Parameterized`](https://junit.org/junit4/javadoc/4.12/org/junit/runners/Parameterized.html)
79runner. If you need to use a different runner for some reason, please reach out
80to the team and we can review/approve the use.
81
82#### Disabling tests
83
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
95#### Filtering devices
96
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
107// Target pre-release SDK R only
108@SdkSuppress(minSdkVersion = Build.VERSION_CODES.R, isCodeName = "R")
109```
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)
113methods.
114
115To restrict to only phsyical devices, use
116[`@RequiresDevice`](https://developer.android.com/reference/androidx/test/filters/RequiresDevice).
117
AndroidX Core Team5c914c42021-02-08 17:22:57 +0000118NOTE [Cuttlefish](https://source.android.com/setup/create/cuttlefish) is not
119affected by this annotation, only e.g. Studio emulators. If Cuttlefish is
120displaying behavior that differs from a physical device, they are considering
121that a bug in Cuttlefish, so please file those bugs instead of only looking for
122a workaround.
123
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000124### Animations in tests
125
126Animations are disabled for tests by default. This helps avoid flakes due to
127timing and also makes tests faster.
128
129In rare cases, like testing the animations themselves, you may want to enable
130animations for a particular test or test class. For those cases, you can use the
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000131[`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 +0000132
133## Using the emulator {#emulator}
134
135You can use the emulator or a real device to run tests. If you wish to use the
136emulator, you will need to access the AVD Manager (and your downloaded emulator
137images) using a separate "normal" instance of Android Studio. "Normal" means a
138non-Canary build of Studio that you would use for regular app development -- the
139important part being that it points to the Android SDK where your downloaded
140emulator images reside. You will need to open a project to get the Tools menu --
141do NOT open the AndroidX project in the "normal" instance of Android Studio;
142instead, open a normal app or create a blank project using the app wizard.
143
144## Debugging with platform SDK sources {#sources}
145
146The platform SDK sources that are checked into the development branch may not
147match up with the build of Android present on the emulator or your physical
148device. As a result, the line numbers reported by the debugger may not match up
149the actual code being run.
150
151If you have a copy of the sources for the build against which you are debugging,
152you can manually specify your platform SDK source path:
153
1541. Click on a module (e.g. `appcompat`) in the `Project` view
1551. Press `Ctrl-Shift-A` and type "Module Settings", then run the action
1561. In the `Project Structure` dialog, navigate to `SDKs > Android API 29
157 Platform > Sourcepath`
1581. Use the `-` button to remove any paths that are present, then use the `+`
159 button to add the desired source path, ex. `<android checkout
160 root>/frameworks/base` if you are debugging against a locally-built system
161 image
162
163NOTE The `Project Structure` dialog reachable via `File > Project Structure` is
164**not** the same as the `Project Structure` dialog that will allow you to
165specify the SDK source path. You must use the "Module Settings" action as
166directed above.
167
168## Running unit and integration tests {#running}
169
170From Android Studio, right-click can be used to run most test targets, including
171source files, classes within a file, or individual test methods but **not**
172entire modules. To run a supported test target, right-click on the test target
173and then click `Run <name of test target>`.
174
175To run tests for an entire module such as `appcompat`, use `Run -> Edit
176configurations...` and use the `+` button to create a new `Android Instrumented
177Tests` configuration. Specify the module to be tested, give it a reasonable name
178(not "All Tests") and click `OK`, then use the `Run` menu to run the
179configuration.
180
181![alt_text](onboarding_images/image2.png "screenshot of run menu")
182
183NOTE If you receive the error `JUnit version 3.8 or later expected` this means
184that Android Studio generated an Android JUnit configuration when you actually
185needed an Android Instrumented Tests configuration. Open the `Run -> Edit
186configurations...` dialog and delete the configuration from Android JUnit, then
187manually add a configuration in Android Instrumented Tests.
188
189### From the command line {#running-from-shell}
190
191Following a successful build, tests may be run against a particular AndroidX
192module using `gradlew`.
193
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000194To run all unit or integration tests in a specific project, run the following
195from `framework/support`:
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000196
197```shell
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000198# Run instrumentation tests on a connected device
199./gradlew <project-name>:connectedAndroidTest --info --daemon
200
201# Run local unit tests
202./gradlew <project-name>:test --info --daemon
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000203```
204
205substituting the Gradle project name (ex. `core`).
206
207To run all integration tests in the specific project and test class you're
208working on, run
209
210```shell
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000211./gradlew <project-name>:connectedAndroidTest --info --daemon \
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000212 -Pandroid.testInstrumentationRunnerArguments.class=<fully-qualified-class>[\#testName]
213```
214
215substituting the Gradle project name (ex. `viewpager`) and fully-qualified class
216name (ex. `androidx.viewpager.widget.ViewPagerTest`) of your test file,
217optionally followed by `\#testName` if you want to execute a single test in that
AndroidX Core Team408c27b2020-12-15 15:57:00 +0000218file. Substitute `test` for `connectedAndroidTest` to run local unit tests.
AndroidX Core Team2e416b22020-12-03 22:58:07 +0000219
220If you see some weird compilation errors such as below, run `./gradlew clean`
221first:
222
223```
224Unknown source file : UNEXPECTED TOP-LEVEL EXCEPTION:
225Unknown source file : com.android.dex.DexException: Multiple dex files define Landroid/content/pm/ParceledListSlice$1;
226```
227
228## Test apps {#testapps}
229
230Library developers are strongly encouraged to write test apps that exercise
231their library's public API surface. Test apps serve multiple purposes:
232
233* Integration testing and validation of API testability, when paired with
234 tests
235* Validation of API usability and developer experience, when paired with a use
236 case or critical user journey
237* Sample documentation, when embedded into API reference docs using the
238 [`@sample` and `@Sampled` annotations](api_guidelines.md#sample-usage)
239
240### Legacy test apps {#testapps-legacy}
241
242We have a set of legacy sample Android applications in projects suffixed with
243`-demos`. These applications do not have tests and should not be used as test
244apps for new APIs, but they may be useful for manual regression testing.
245
2461. Click `Run/Debug Configuration` on the top of the window.
2471. Select the app you want to run.
2481. Click 'Run' button.
249
250![alt_text](onboarding_images/image3.png "screenshot of Run/Debug menu")
251
252## Benchmarking {#benchmarking}
253
254AndroidX supports benchmarking - locally with Studio/Gradle, and continuously in
255post-submit. For more information on how to create and run benchmarks, see
256[Benchmarking](benchmarking.md).