| name | run_tests |
| description | A skill for identifying and running tests (Unit, Instrumentation, FTL) in the AndroidX repository. |
Run Tests Skill
This skill provides comprehensive instructions for executing tests within the AndroidX repository. It covers module discovery, unit testing, instrumentation testing on connected devices, and remote testing via Firebase Test Lab (FTL).
1. How to Find and Run a Specific Test
If you have a specific failing test (e.g., BasicTextFieldTest#longText_doesNotCrash_singleLine) and want to execute it:
- Find the Test File: Use code search,
find_declaration, or find_files (e.g., search for BasicTextFieldTest.kt).
- Identify the Module: The file path determines the Gradle project (e.g.,
compose/foundation/foundation/src/... belongs to :compose:foundation:foundation).
- Identify Test Type:
- If the test is in a
test, androidHostTest, or jvmTest folder, it is a Unit Test.
- If the test is in
androidTest or androidDeviceTest, it is an Instrumentation Test.
- Identify Module Type: Check if the module is KMP or standard Android (see details below) by inspecting
build.gradle for androidXMultiplatform { or running ./gradlew <module>:tasks | grep "test".
- Formulate the Task: Select the proper Gradle task (e.g.,
testAndroidHostTest vs test, connectedAndroidDeviceTest vs connectedAndroidTest).
- Filter by Class/Method:
- Class: Append filtering arguments. E.g., for instrumentation:
-Pandroid.testInstrumentationRunnerArguments.class=androidx...BasicTextFieldTest. For unit tests: --tests "androidx...BasicTextFieldTest".
- Method: For instrumentation, append
#<method_name> to the class name (e.g., ...BasicTextFieldTest#longText_doesNotCrash_singleLine). For unit tests, append .<method_name> to the class name in --tests (e.g., --tests "...BasicTextFieldTest.longText_doesNotCrash_singleLine").
2. Module Discovery
Before running tests, you must identify the Gradle project name (module) associated with the code you are testing.
- Mapping a path to a project: Most directories in this repository are Gradle projects. Use the directory structure as a guide (e.g.,
appcompat/appcompat corresponds to :appcompat:appcompat).
- Verification: Run
./gradlew projects to see a full list of projects. Because the output is very long, consider chaining a grep command to filter the output if you are looking for a specific module (e.g., ./gradlew projects | grep appcompat).
- Module Type: Be aware if the module is a standard Android library (like
appcompat) or a Kotlin Multiplatform (KMP) library (like compose:foundation:foundation). Task names differ significantly. To determine if a module is KMP, you can run ./gradlew <project-name>:tasks | grep "test": if you see tasks like testAndroidHostTest or jvmTest, it is a KMP module. If you see standard test and connectedAndroidTest tasks, it is a standard Android module. Alternatively, inspect its build.gradle file for androidXMultiplatform {.
3. Unit Testing (JVM)
Unit tests run on the host machine and are the fastest way to verify logic.
- Standard Android Modules:
./gradlew <project-name>:test
- KMP Modules:
./gradlew <project-name>:testAndroidHostTest
./gradlew <project-name>:jvmStubsTest
- Filtering:
Use the
--tests filter. You can also append .<method_name> for specific methods.
./gradlew <project-name>:test --tests "androidx.example.MyTest"
./gradlew <project-name>:testAndroidHostTest --tests "androidx.example.MyTest"
./gradlew <project-name>:test --tests "androidx.example.MyTest.myMethod"
4. Instrumentation Testing (Connected Devices)
These tests run on a physical device or emulator connected via ADB.
- Standard Android Modules:
./gradlew <project-name>:connectedAndroidTest
- KMP Modules:
./gradlew <project-name>:connectedAndroidDeviceTest
- Filtering:
Use the
android.testInstrumentationRunnerArguments.class property. For specific methods, append #<method_name>.
./gradlew <project-name>:connectedAndroidTest \
-Pandroid.testInstrumentationRunnerArguments.class=androidx.example.MyTest
./gradlew <project-name>:connectedAndroidDeviceTest \
-Pandroid.testInstrumentationRunnerArguments.class=androidx.example.MyTest#myMethod
5. Remote Testing (Firebase Test Lab)
AndroidX provides specialized tasks for running instrumentation tests on Firebase Test Lab (FTL). FTL is not used for local unit tests. The task suffix changes based on module type.
5.1. Reproducing Flakes on FTL
To verify a flaky test, you can run it multiple times on Firebase Test Lab using the ftlOnApis task variant.
- Parameterize the Test: To run a test N times, temporarily modify the test class to be parameterized:
- Run Locally (Optional): Verify it runs a few times locally before deploying to FTL. E.g., for KMP:
./gradlew <project-name>:connectedAndroidDeviceTest -Pandroid.testInstrumentationRunnerArguments.class=androidx.example.MyTest
- Run on FTL: Use the
ftlOnApis task. Specify the API level(s) with --api and add a longer timeout --testTimeout=1h (if the API level is not provided, you must ask the user for it).
./gradlew <project-name>:ftlOnApisandroidDeviceTest --testTimeout=1h --api 28 --api 30 --className=androidx.example.MyTest
6. Screenshot / Visual Tests
Screenshot tests (e.g., using AndroidXScreenshotTestRule, which extends the core ScreenshotTestRule.kt) compare the rendered UI against approved "golden" reference images to detect visual regressions.
-
Emulator Requirement: Screenshot tests must be executed on a specific emulator configuration to ensure consistent rendering.
- Verify the required configuration: If the required emulator configuration is not found in the test class or local documentation, fall back to verifying it by reading
ScreenshotTestRule.kt. Check the ScreenshotTestStatement class for the required API level (typically API 35) and device model.
- Locally, a Medium Phone API 35 emulator is typically required.
-
Managing the Emulator:
-
Running the Tests: Once the emulator is running (or if using FTL), execute the screenshot tests. If running locally, they are executed as standard instrumentation tests using Gradle (see Section 4).
-
Troubleshooting Missing Screenshots:
- If the tests run but screenshots do not show up or cannot be pulled, follow the code in the test and
ScreenshotTestRule.kt to understand how they are configured and where they are being saved on the device.
-
Updating Screenshot Goldens:
If a screenshot test fails due to intentional UI changes, you must update the golden reference images in the support-goldens repository (which is checked out as a sibling directory to ).