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