Skip to content

Different appraoaches to combine Robolectric & Espresso

Nico Küchler edited this page Mar 17, 2015 · 7 revisions

Obsolete

Most of the issues are gone and no more plugin is necessary.

There exist other ways to accomplish this setup

I tried different approaches to combine android studio with robolectric & espresso. But at the end i ended with my promoted setup, because of to many drawbacks with the others.

Here some explanation for the different approaches:

application module + espresso + robolectric (JCAndKSolutions)

A great working solution https://github.com/JCAndKSolutions/android-unit-test

application module + espresso + robolectric (robolectric)

There is an example https://github.com/robolectric/deckard-gradle supported by robolectric maintainers. This is based on the plugin https://github.com/robolectric/gradle-android-test-plugin. But this have a drawback with dependency pollution reported at https://github.com/robolectric/gradle-android-test-plugin/issues/17 which results in slow esspresso tests compile time and execution time.

build.gradle snippet which combines all

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.10.+'
        classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.+'
    }
}

apply plugin: 'android'
apply plugin: 'android-test'

android {
    defaultConfig {
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }
}

androidTest {
    include '**/*Test.class'
    exclude '**/espresso/**/*.class'
}

dependencies {
    androidTestCompile('junit:junit:4.11')
    androidTestCompile('org.robolectric:robolectric:2.3-SNAPSHOT')
    androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}

seperate espresso

An example is shown by https://github.com/stephanenicolas/Quality-Tools-for-Android but it is much outdated and had also some drawbacks. It will recompile and makes android studio behave strange. It flags application module sources as (root source) of the espresso test module. That works but not intuitive.

build.gradle snippet for espresso module

dependencies {
    androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}

android {
    sourceSets {
        main {
            manifest.srcFile '../AndroidSample/AndroidManifest.xml'
            java.srcDirs += ['../AndroidSample/src/main/java']
            resources.srcDirs = ['../AndroidSample/res']
            res.srcDirs = ['../AndroidSample/res']
        }
    }
    defaultConfig {
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }

}

See also this thread https://github.com/nenick/android-gradle-template/issues/2

separate robolectric

There exist a plugin https://github.com/novoda/gradle-android-test-plugin which enable us to put robolectric tests in a sperate package. This project setup works for me great:

- MyProject 
|- app (with espresso tests)
|- - build.gradle (app)
|- robolectric (unit tests)
|- - build.gradle (robo)

build.gradle (app + espresso) snippet

dependencies {
    androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}            

android {
    defaultConfig {
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }
} 

build.gradle (robo) snippet

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
        classpath "com.novoda:gradle-android-test-plugin:0.9.8-SNAPSHOT"
    }
}

android {
    projectUnderTest ':AndroidSample'
}

apply plugin: 'java'
apply plugin: 'android-test'

dependencies {
    testCompile 'junit:junit:4.11'
    testCompile 'org.mockito:mockito-core:1.9.5'
    testCompile 'com.squareup:fest-android:1.0.+')
    testCompile ('org.robolectric:robolectric:2.3-SNAPSHOT')
}