android tutorial - Espresso setup instructions in Android Espresso | Developer android - android app development - android studio - android app developement



Espresso setup instructions

  • Setup your test environment
  • Download Espresso
  • Set the instrumentation runner
  • Example build.gradle file
  • Analytics
  • Add the first test
  • Running tests This guide covers installing Espresso using the SDK Manager and building it using Gradle. Android Studio is recommended.

Setup your test environment

  • To avoid flakiness, we highly recommend that you turn off system animations on the virtual or physical device(s) used for testing.

On your device, under Settings->Developer options disable the following 3 settings:

  • Window animation scale
  • Transition animation scale
  • Animator duration scale

Download Espresso

  • Make sure you have installed the latest Android Support Repository under Extras (see instructions).
  • Open your app’s build.gradle file. This is usually not the top-level build.gradle file but app/build.gradle.
  • Add the following lines inside dependencies:
  • androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' androidTestCompile 'com.android.support.test:runner:0.5'
  • See the downloads section for more artifacts (espresso-contrib, espresso-web, etc.)
  • Set the instrumentation runner

Add to the same build.gradle file the following line in android.defaultConfig: testInstrumentationRunner

"android.support.test.runner.AndroidJUnitRunner"
click below button to copy code from our android learning website - android tutorial - team
  • Example build.gradle file
apply plugin: 'com.android.application'

    android {
        compileSdkVersion 22
        buildToolsVersion "22"
    
        defaultConfig {
            applicationId "com.my.awesome.app"
            minSdkVersion 10
            targetSdkVersion 22.0.1
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    }

dependencies {
    // App's dependencies, including test
    compile 'com.android.support:support-annotations:22.2.0'

    // Testing-only dependencies
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
click below button to copy code from our android learning website - android tutorial - team

Analytics

  • In order to make sure we are on the right track with each new release, the test runner collects analytics. More specifically, it uploads a hash of the package name of the application under test for each invocation. This allows us to measure both the count of unique packages using Espresso as well as the volume of usage.
  • If you do not wish to upload this data, you can opt out by passing the following argument to the test runner: disableAnalytics "true" (see how to pass custom arguments).
  • Add the first test
  • Android Studio creates tests by default in src/androidTest/java/com.example.package/

Example JUnit4 test using Rules:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class HelloWorldEspressoTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);

    @Test
    public void listGoesOverTheFold() {
        onView(withText("Hello world!")).check(matches(isDisplayed()));
    }
}
click below button to copy code from our android learning website - android tutorial - team

Running tests

In Android Studio

Create a test configuration In Android Studio:

  • Open Run menu -> Edit Configurations
  • Add a new Android Tests configuration
  • Choose a module
  • Add a specific instrumentation runner:
  • android.support.test.runner.AndroidJUnitRunner

Run the newly created configuration. From command-line via Gradle Execute

./gradlew connectedAndroidTest
click below button to copy code from our android learning website - android tutorial - team

Espresso has basically three components:

  • Open Run menu -> Edit Configurations
  • Add a new Android Tests configuration
  • Choose a module
  • Add a specific instrumentation runner:
  • android.support.test.runner.AndroidJUnitRunner

Base Espresso Test

onView(ViewMatcher)       -- 1     
 .perform(ViewAction)     -- 2
   .check(ViewAssertion); -- 3
click below button to copy code from our android learning website - android tutorial - team
  • Finds the view
  • Performs an action on the view
  • Validates a assertioin

Related Searches to Espresso setup instructions in Android Espresso