Android Testing Samples

repository·main·Indexed 27 days ago

https://github.com/android/testing-samples

A collection of Android testing samples demonstrating frameworks and techniques for automated testing. Includes examples for Espresso (UI testing, custom matchers, IdlingResource, Espresso-web), UiAutomator (black-box testing), AndroidJUnitRunner (parameterized tests, test suites), and JUnit4 Rules such as ActivityTestRule, IntentsTestRule, and ServiceTestRule. Provides setup and execution guides for Gradle and experimental Bazel builds.

Tokens
7.8K
Snippets
6
Records
55
Agent score
94%

What's inside android-testing-samples

  1. Overview of Espresso Web

    main

    Espresso Web is an API designed for writing automated tests for hybrid applications containing one or more WebViews.

    Key characteristics:

    • Interaction Model: Similar to onData, WebView interactions are composed of several ViewActions. However, in Espresso Web, ViewActions are composed of Web Driver Atoms.
    • Synchronization: Espresso Web handles synchronization automatically to minimize boilerplate.
    • Developer Experience: It provides an Espresso-like syntax for interacting with web content within an Android application.
  2. Overview of Android testing samples

    main

    This repository is a collection of samples demonstrating various Android testing frameworks and techniques. Key sample categories include:

    • Espresso Samples: Covers basic UI testing, custom matchers, onData() for lists, FragmentScenario, IdlingResource for synchronization, Intents (basic and advanced), multi-window testing, RecyclerView actions, screenshot capturing, and Espresso-web for WebViews.
    • UiAutomator Samples: Demonstrates basic UI Automator usage for black-box testing.
    • AndroidJUnitRunner Samples: Showcases test annotations, parameterized tests, and testsuite creation.
    • JUnit4 Rules Samples: Demonstrates usage of ActivityTestRule, IntentsTestRule, and ServiceTestRule.
  3. Overview of Espresso Intents Advanced Sample

    main

    This sample demonstrates how to use Espresso Intents for hermetic inter-app testing. It utilizes a Mockito-like approach to allow for Intent verification and stubbing.

    Specifically, this sample shows how to stub an Intent to simulate fetching a picture from the camera without actually leaving the main application, ensuring tests remain isolated and hermetic.

  4. Use AndroidJUnitRunner for JUnit4 testing

    main

    The androidx.test.runner.AndroidJUnitRunner enables JUnit4 support for Android testing, allowing for features such as:

    • Test Annotations: Standard JUnit4 style annotations.
    • Parameterized Tests: Using the @Parameters annotation to run a single test with multiple different values.
    • Test Suites: Grouping tests into suites such as AndroidTestSuite, UnitTestSuite, or InstrumentationTestSuite.
    • Instrumentation Tests: Testing the UI of activities (e.g., CalculatorActivity) using JUnit4 style tests.
  5. Set up the Data Adapter Espresso Sample

    main

    Follow these steps to build and run the Data Adapter sample project:

    1. Clone the repository: Download the project code using git clone.
    2. Install Dependencies: Open the Android SDK Manager and ensure the Android testing support library Repository is installed under Extras.
    3. Open Project: In Android Studio, select File | Open... and select the ./build.gradle file.
    4. Configure Test Runner: Create a custom test configuration in Android Studio:
      • Go to Run menu | Edit Configurations.
      • Add a new Android Tests configuration.
      • Select the appropriate module.
      • Set the Specific instrumentation runner to androidx.test.runner.AndroidJUnitRunner.
    5. Prepare Device: Connect a physical device or start an emulator. Disable animations in the device's Developer options to ensure test stability:
      • Disable Window animation scale
      • Disable Transition animation scale
      • Disable Animator duration scale
    6. Run Tests: Execute the newly created configuration. Results will appear in the Android Studio Run window.