I tried different appraoaches to combine android studio & robolectric & espresso. I ended with this example project setup https://github.com/nenick/android-gradle-template
Here some explanation for the different approaches:
application module + espresso + 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"
}
}
spereate 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')
}
There a some pitfall when you try setup this project setup, so just start with a working example: https://github.com/nenick/android-gradle-template