blob: b937bb959a822461dd84e91f162d13a1a50583b4 [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
17[`build.gradle`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:preference/preference/build.gradle)
18and
19[`EditTextPreferenceTest.java`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:preference/preference/src/androidTest/java/androidx/preference/tests/EditTextPreferenceTest.java)
20files.
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
30[AffectedModuleDetector](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:buildSrc/src/main/kotlin/androidx/build/dependencyTracker/AffectedModuleDetector.kt)
31to 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
118### Animations in tests
119
120Animations are disabled for tests by default. This helps avoid flakes due to
121timing and also makes tests faster.
122
123In rare cases, like testing the animations themselves, you may want to enable
124animations for a particular test or test class. For those cases, you can use the
125[`AnimationDurationScaleRule`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:testutils/testutils-runtime/src/main/java/androidx/testutils/AnimationDurationScaleRule.kt).
126
127## Using the emulator {#emulator}
128
129You can use the emulator or a real device to run tests. If you wish to use the
130emulator, you will need to access the AVD Manager (and your downloaded emulator
131images) using a separate "normal" instance of Android Studio. "Normal" means a
132non-Canary build of Studio that you would use for regular app development -- the
133important part being that it points to the Android SDK where your downloaded
134emulator images reside. You will need to open a project to get the Tools menu --
135do NOT open the AndroidX project in the "normal" instance of Android Studio;
136instead, open a normal app or create a blank project using the app wizard.
137
138## Debugging with platform SDK sources {#sources}
139
140The platform SDK sources that are checked into the development branch may not
141match up with the build of Android present on the emulator or your physical
142device. As a result, the line numbers reported by the debugger may not match up
143the actual code being run.
144
145If you have a copy of the sources for the build against which you are debugging,
146you can manually specify your platform SDK source path:
147
1481. Click on a module (e.g. `appcompat`) in the `Project` view
1491. Press `Ctrl-Shift-A` and type "Module Settings", then run the action
1501. In the `Project Structure` dialog, navigate to `SDKs > Android API 29
151 Platform > Sourcepath`
1521. Use the `-` button to remove any paths that are present, then use the `+`
153 button to add the desired source path, ex. `<android checkout
154 root>/frameworks/base` if you are debugging against a locally-built system
155 image
156
157NOTE The `Project Structure` dialog reachable via `File > Project Structure` is
158**not** the same as the `Project Structure` dialog that will allow you to
159specify the SDK source path. You must use the "Module Settings" action as
160directed above.
161
162## Running unit and integration tests {#running}
163
164From Android Studio, right-click can be used to run most test targets, including
165source files, classes within a file, or individual test methods but **not**
166entire modules. To run a supported test target, right-click on the test target
167and then click `Run <name of test target>`.
168
169To run tests for an entire module such as `appcompat`, use `Run -> Edit
170configurations...` and use the `+` button to create a new `Android Instrumented
171Tests` configuration. Specify the module to be tested, give it a reasonable name
172(not "All Tests") and click `OK`, then use the `Run` menu to run the
173configuration.
174
175![alt_text](onboarding_images/image2.png "screenshot of run menu")
176
177NOTE If you receive the error `JUnit version 3.8 or later expected` this means
178that Android Studio generated an Android JUnit configuration when you actually
179needed an Android Instrumented Tests configuration. Open the `Run -> Edit
180configurations...` dialog and delete the configuration from Android JUnit, then
181manually add a configuration in Android Instrumented Tests.
182
183### From the command line {#running-from-shell}
184
185Following a successful build, tests may be run against a particular AndroidX
186module using `gradlew`.
187
188To run all integration tests in a specific project, run the following from
189`framework/support`:
190
191```shell
192./gradlew <project-name>:connectedCheck --info --daemon
193```
194
195substituting the Gradle project name (ex. `core`).
196
197To run all integration tests in the specific project and test class you're
198working on, run
199
200```shell
201./gradlew <project-name>:connectedCheck --info --daemon \
202 -Pandroid.testInstrumentationRunnerArguments.class=<fully-qualified-class>[\#testName]
203```
204
205substituting the Gradle project name (ex. `viewpager`) and fully-qualified class
206name (ex. `androidx.viewpager.widget.ViewPagerTest`) of your test file,
207optionally followed by `\#testName` if you want to execute a single test in that
208file.
209
210If you see some weird compilation errors such as below, run `./gradlew clean`
211first:
212
213```
214Unknown source file : UNEXPECTED TOP-LEVEL EXCEPTION:
215Unknown source file : com.android.dex.DexException: Multiple dex files define Landroid/content/pm/ParceledListSlice$1;
216```
217
218## Test apps {#testapps}
219
220Library developers are strongly encouraged to write test apps that exercise
221their library's public API surface. Test apps serve multiple purposes:
222
223* Integration testing and validation of API testability, when paired with
224 tests
225* Validation of API usability and developer experience, when paired with a use
226 case or critical user journey
227* Sample documentation, when embedded into API reference docs using the
228 [`@sample` and `@Sampled` annotations](api_guidelines.md#sample-usage)
229
230### Legacy test apps {#testapps-legacy}
231
232We have a set of legacy sample Android applications in projects suffixed with
233`-demos`. These applications do not have tests and should not be used as test
234apps for new APIs, but they may be useful for manual regression testing.
235
2361. Click `Run/Debug Configuration` on the top of the window.
2371. Select the app you want to run.
2381. Click 'Run' button.
239
240![alt_text](onboarding_images/image3.png "screenshot of Run/Debug menu")
241
242## Benchmarking {#benchmarking}
243
244AndroidX supports benchmarking - locally with Studio/Gradle, and continuously in
245post-submit. For more information on how to create and run benchmarks, see
246[Benchmarking](benchmarking.md).