Android Testing Templates

repository·master·Indexed 24 days ago

https://github.com/googlesamples/android-testing-templates

A 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.

Tokens
1.6K
Snippets
0
Records
11
Agent score
84%

What's inside android-testing-templates

  1. Espresso Testing Frameworks

    master

    Espresso 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 WebView content on Android.
  2. Pass custom arguments to AndroidJUnitRunner via Gradle

    master

    You can pass custom arguments to AndroidJUnitRunner using the -Pandroid.testInstrumentationRunnerArguments property 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=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
  3. Use the Android Testing Blueprint for integrated testing frameworks

    master
    The 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.
  4. Run Unit Tests

    master

    Unit tests (local tests) run on your development machine's JVM.

    In Android Studio

    1. Open the Build Variants window and ensure Unit Tests is selected.
    2. Open a local test class (e.g., LocalUnitTest.java).
    3. Right-click the class and select Run as JUnit test.

    Via Gradle (Command Line)

    • Run all unit tests in the app module:
      ./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 --tests with ./gradlew app:test. You must use the specific variant task.
      ./gradlew :app:testFlavor1DebugUnitTest --tests "com.example.android.testing.blueprint.*"
      In a project without flavors:
      ./gradlew :{module}:testDebugUnitTest --tests {test-filter}
  5. Run Instrumentation Tests

    master

    Instrumentation tests run on a physical device or emulator. You can execute them via Android Studio or the command line.

    In Android Studio

    1. Open the Build Variants window and ensure Android Instrumentation Tests is selected.
    2. Open an instrumentation test class (e.g., EspressoTest.java).
    3. Right-click the class and select Run as Android Test.

    Via Gradle (Command Line)

    To run all instrumentation tests in the app module:

    ./gradlew app:connectedAndroidTest

    Via adb (Command Line)

    1. Install both the app and the test APK:
    ./gradlew app:installFlavor2Debug app:installFlavor2DebugAndroidTest
    1. Execute the instrumentation command:
    adb shell am instrument -w com.example.android.testing.blueprint.flavor2.test/android.support.test.runner.AndroidJUnitRunner
  6. Generate Code Coverage Reports

    master

    Unit 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:
      ./gradlew module-plain-java:test module-plain-java:jacocoTestReport
      The report is located at: module-plain-java/build/reports/jacoco/test/html/index.html

    Android Test Coverage

    • Coverage reports for instrumentation tests are located in: app/build/reports/coverage.
  7. Pass Custom Arguments to AndroidJUnitRunner

    master

    You can pass custom arguments to AndroidJUnitRunner via Gradle using the -Pandroid.testInstrumentationRunnerArguments property. 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
  8. Use an Android Test-Only Module

    master

    To separate production code from test code, you can use a test-only module.

    Key Characteristics:

    • All test code is placed in the main source set (src/main/java) instead of androidTest.
    • Requires an AndroidManifest.xml in src/main.
    • Cannot have a src/androidTest folder (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:
      1. Install app and test module:
        ./gradlew app:installFlavor1Debug module-flavor1-androidTest-only:installDebug
      2. Run instrumentation:
        adb shell am instrument -w com.example.android.testing.blueprint.test/android.support.test.runner.AndroidJUnitRunner