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, orfind_files(e.g., search forBasicTextFieldTest.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, orjvmTestfolder, it is a Unit Test. - If the test is in
androidTestorandroidDeviceTest, it is an Instrumentation Test.
- If the test is in a
- Identify Module Type: Check if the module is KMP or standard Android (see details below) by inspecting
build.gradleforandroidXMultiplatform {or running./gradlew <module>:tasks | grep "test". - Formulate the Task: Select the proper Gradle task (e.g.,
testAndroidHostTestvstest,connectedAndroidDeviceTestvsconnectedAndroidTest). - 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").
- Class: Append filtering arguments. E.g., for instrumentation:
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/appcompatcorresponds to:appcompat:appcompat). - Verification: Run
./gradlew projectsto see a full list of projects. Because the output is very long, consider chaining agrepcommand 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 (likecompose: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 liketestAndroidHostTestorjvmTest, it is a KMP module. If you see standardtestandconnectedAndroidTesttasks, it is a standard Android module. Alternatively, inspect itsbuild.gradlefile forandroidXMultiplatform {.
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
--testsfilter. 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.classproperty. 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.
- Standard Android Modules:
ftl<device><api>releaseAndroidTest - KMP Modules:
ftl<device><api>androidDeviceTest - Listing Available Combinations:
To see the full list of available FTL tasks for a specific project, you can run:
./gradlew <project-name>:tasks --all | grep ftl - Common Device/API combinations:
mediumphoneapi36mediumphoneapi35mediumphoneapi34mediumphoneapi30nexus5api23
- Example (KMP):
./gradlew <project-name>:ftlmediumphoneapi35androidDeviceTest --className="androidx.example.MyTest"
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:
- Add
@RunWith(Parameterized::class)to the class. - Update the constructor to accept a repetition parameter:
class MyTest(private val repetition: Int). - Add the companion object for data generation:
import org.junit.runners.Parameterized companion object { private const val RUNS = 100 // Adjust as needed @JvmStatic @Parameterized.Parameters fun data(): Array<Int> = Array(RUNS) { 0 } } - Note: If there are many tests in the class but you only want to focus on one, comment out the
@Testannotation on the irrelevant ones. DO NOT REPLACE ANY LINES OF CODE OR VARIABLES NAMES in the test class aside from making it parameterized. Test class name should remain the same.
- Add
- 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
ftlOnApistask. Specify the API level(s) with--apiand add a longer timeout--testTimeout=1h(if the API level is not provided, you must ask the user for it).# Example for KMP (append 'releaseAndroidTest' instead of 'androidDeviceTest' for standard Android) ./gradlew <project-name>:ftlOnApisandroidDeviceTest --testTimeout=1h --api 28 --api 30 --className=androidx.example.MyTest