Android Testing Templates
repository·master·Indexed 24 days ago
https://github.com/googlesamples/android-testing-templatesA reference implementation known as the 'Android Testing Blueprint' that integrates Google Android testing tools and frameworks into a single application. It provides guidance on using Espresso (Core, Contrib, Intents, Web), UIAutomator, and the Android Testing Support Library (ATSL). The documentation covers running instrumentation and unit tests via Android Studio, Gradle, and adb, managing test-only modules, generating JaCoCo code coverage reports, and passing custom arguments to AndroidJUnitRunner.
What's inside android-testing-templates
- UI Automator is a framework for building UI tests that interact with both user apps and system apps (e.g., opening the Settings menu or the app launcher). It is designed for black-box testing, meaning the test code does not rely on the internal implementation details of the target application.
Espresso Testing Frameworks
masterEspresso is part of the Android Testing Support Library (ATSL) for writing reliable UI tests. The project utilizes several Espresso extensions:
- Espresso-Core: The fundamental API for testing state expectations, interactions, and assertions on the UI. It automatically synchronizes with the application UI state.
- Espresso-Contrib: Provides support for common Android widgets and extensions, such as
RecyclerView. - Espresso-Intents: Enables hermetic inter-app testing by allowing Intent verification and stubbing (similar to Mockito).
- Espresso-Web: Uses the WebDriver API to test and control
WebViewcontent on Android.
Pass custom arguments to AndroidJUnitRunner via Gradle
masterYou can pass custom arguments to
AndroidJUnitRunnerusing the-Pandroid.testInstrumentationRunnerArgumentsproperty in Gradle. This is useful for filtering tests by size, class, or passing arbitrary values to tests at runtime.Examples
Run tests with a specific size qualifier (e.g., @Large):
./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.size=largeRun a specific test class:
./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.android.testing.blueprint.ui.espresso.EspressoTestPass an arbitrary argument to be accessed at runtime:
./gradlew module-flavor1-androidTest-only:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.argument1=make_test_failUse the Android Testing Blueprint for integrated testing frameworks
masterThe Android Testing Blueprint provides a single application project that integrates a collection of Google's Android testing tools and frameworks. You can choose between a Java implementation or a Kotlin implementation to see how these tools work together in a real-world application structure.Run Unit Tests
masterUnit tests (local tests) run on your development machine's JVM.
In Android Studio
- Open the Build Variants window and ensure Unit Tests is selected.
- Open a local test class (e.g.,
LocalUnitTest.java). - Right-click the class and select Run as JUnit test.
Via Gradle (Command Line)
- Run all unit tests in the
appmodule:./gradlew app:test - Run all unit tests in a specific module:
./gradlew :{module}:test - Run filtered unit tests (by package/class):
Note: You cannot use
--testswith./gradlew app:test. You must use the specific variant task.
In a project without flavors:./gradlew :app:testFlavor1DebugUnitTest --tests "com.example.android.testing.blueprint.*"./gradlew :{module}:testDebugUnitTest --tests {test-filter}
Run Instrumentation Tests
masterInstrumentation tests run on a physical device or emulator. You can execute them via Android Studio or the command line.
In Android Studio
- Open the Build Variants window and ensure Android Instrumentation Tests is selected.
- Open an instrumentation test class (e.g.,
EspressoTest.java). - Right-click the class and select Run as Android Test.
Via Gradle (Command Line)
To run all instrumentation tests in the
appmodule:./gradlew app:connectedAndroidTestVia adb (Command Line)
- Install both the app and the test APK:
./gradlew app:installFlavor2Debug app:installFlavor2DebugAndroidTest- Execute the instrumentation command:
adb shell am instrument -w com.example.android.testing.blueprint.flavor2.test/android.support.test.runner.AndroidJUnitRunnerGenerate Code Coverage Reports
masterUnit Test Coverage
- Android Studio: Use the Run test with code coverage option. Reports appear next to lines in the editor.
- Gradle (JaCoCo): To generate HTML reports for a Java module:
The report is located at:./gradlew module-plain-java:test module-plain-java:jacocoTestReportmodule-plain-java/build/reports/jacoco/test/html/index.html
Android Test Coverage
- Coverage reports for instrumentation tests are located in:
app/build/reports/coverage.
Pass Custom Arguments to AndroidJUnitRunner
masterYou can pass custom arguments to
AndroidJUnitRunnervia Gradle using the-Pandroid.testInstrumentationRunnerArgumentsproperty. This is useful for filtering specific tests or passing runtime flags to your tests.Examples
- Run tests with a specific size qualifier (e.g., @Large):
./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.size=large - Run a specific test class:
./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.android.testing.blueprint.ui.espresso.EspressoTest - Pass an arbitrary argument to be accessed at runtime:
./gradlew module-flavor1-androidTest-only:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.argument1=make_test_fail
- Run tests with a specific size qualifier (e.g., @Large):
Use an Android Test-Only Module
masterTo separate production code from test code, you can use a test-only module.
Key Characteristics:
- All test code is placed in the
mainsource set (src/main/java) instead ofandroidTest. - Requires an
AndroidManifest.xmlinsrc/main. - Cannot have a
src/androidTestfolder (as it cannot produce a test APK). - Only supports Instrumentation-based tests.
- You must create a separate test-only module for each application flavor.
Running Test-Only Module Tests
- Via Gradle:
./gradlew module-flavor1-androidTest-only:connectedAndroidTest - Via adb:
- Install app and test module:
./gradlew app:installFlavor1Debug module-flavor1-androidTest-only:installDebug - Run instrumentation:
adb shell am instrument -w com.example.android.testing.blueprint.test/android.support.test.runner.AndroidJUnitRunner
- Install app and test module:
- All test code is placed in the
Access additional Android testing samples
masterFor a broader collection of samples demonstrating various automated testing frameworks and techniques beyond the integrated blueprint, refer to the dedicated Android Testing samples repository.Explore the Android Testing Support Library (ATSL)
masterThe testing templates in this repository utilize the Android Testing Support Library (ATSL). For detailed documentation, API references, and more information on using these libraries in your own projects, visit the official ATSL site.