How to install Espresso and Cucumber in Android Studio

You can download a sample project that is already configured and try first:

  • The structure of your project should be like this:

On Android View:

Screen Shot 2015-09-12 at 15.14.59

On Project View:

Screen Shot 2015-09-12 at 15.14.46


  • Now, open the build.gradle of your app and write these dependencies:
dependencies {
 androidTestCompile ''
 androidTestCompile ''
 androidTestCompile 'info.cukes:cucumber-android:1.2.4'
 androidTestCompile 'info.cukes:cucumber-picocontainer:1.2.4'
  • I had some problems with the version of java, if you have the same problem just update your java or downgrade/upgrade the version of the plugin which is incompatible.
  • In your build.gradle you will need to write more these configs. Change the name of your application and the package of the runner, following the structure of your project and sync your build.gradle file.
android {
 defaultConfig {
 testApplicationId "com.example.azevedorafaela.myapplication"
 testInstrumentationRunner "com.example.azevedorafaela.myapplication.test.
   sourceSets {
     androidTest {
        assets.srcDirs = ['src/androidTest/assets']
  • In your Feature file you an write your scenario like this:
Feature: Test

Scenario: Espresso with cucumber test
Given I have my app configured
When something happens
Then I should see xx on the display
  • In your Instrumentation class:
package com.example.azevedorafaela.myapplication.test;
import android.os.Bundle;
public class Instrumentation extends MonitoringInstrumentation {
private final CucumberInstrumentationCore instrumentationCore = new 
 public void onCreate(final Bundle bundle) {
 public void onStart() {
  • In your Steps file:
package com.example.azevedorafaela.myapplication.test;

import android.test.ActivityInstrumentationTestCase2;

import com.example.azevedorafaela.myapplication.MainActivity;

import cucumber.api.CucumberOptions;
import com.example.azevedorafaela.myapplication.R;
import static;
import static;
import static
import static;
import static;

@CucumberOptions(features = "features")
public class MainActivitySteps extends ActivityInstrumentationTestCase2
<MainActivity> {

    public MainActivitySteps(){

    @Given("^I have my app configured$")
    public void I_have_my_app_configured() {

    @When("^something happens$")
    public void something_happens(final char op) {


    @Then("^I should see xx on the display$")
    public void I_should_see_xx_on_the_display(final String s) {


Now you can start write your espresso code inside of each step. To run your test you need to:

  • In your terminal, open your project folder and run:
gradle --parallel :app:assembleDebugTest
  •  Now you need istall the apk in your device/simulator:
adb install -r app/build/outputs/apk/app-debug.apk
  • Check if your app is installed. Should display the instrumentation of your app
adb shell pm list instrumentation
  • To run the tests with gradle:
gradle connectedCheck
  • To run the tests with adb:
adb shell am instrument -w com.example.azevedorafaela.myapp
  • To run your tests via android configurations:
    1. Open your run configurations
    2. Create an androidTests
    3. Give a name to this config
    4. Select your module
    5. Don’t write anything on the instrumentation field. (We already configured this in our build.gradle)
    6. Ok.

Screen Shot 2015-09-12 at 15.39.22


Now you can run your cucumber with espresso tests. I hope this “tutorial” helps you as helped me to install everything on my project.

Thank you guys ! See you next week 🙂

4 thoughts on “How to install Espresso and Cucumber in Android Studio

  1. Hi rafael, I tried something on the similar lines, but, getting the following exception

    FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ‘:app:preDexDebugAndroidTest’.
    > org.gradle.process.internal.ExecException: Process ‘command ‘/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java” finished with non-zero exit value 1

    Do you have any idea why this is happening?

    1. Hi Bindu, sorry the delay to reply to you, I was on holidays… So, I had the same problem and I took ages to figure out that is because of the incompatible versions between the plugins. Just have a look what is the version of your Espresso and Cucumber-JVM and try with another versions 🙂

  2. Hi Rafaela,

    Thank you for sharing this information. What I don’t understand in your tutorial is the last step. I mean, I know what are run/debug configurations, but I don’t understand why do you add a new configuration for Android Tests and not for Cucumber.

    Lets say I add that Android Tests configuration. Then I open a .feature file, try to run a Scenario… and I can’t select that run/debug configuration because Android Studio only shows Cucumber configurations. Can you please make it more clear to me?

    Thank you!

    1. Hi Andre, when I wrote this tutorial the Cucumber was not integrated on Android Studio 😦 So, I had to set everything with Android Tests, but you can set the configurations using Cucumber as well… 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.