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
- Example build.gradle file
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:
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
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
- Finds the view
- Performs an action on the view
- Validates a assertioin