I have an Android project which uses Robolectric for testing. The relevant parts of build.gradle look a bit like this:
apply plugin: 'robolectric'
robolectric {
include '**/*Test.class'
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('junit:junit:4.12') {
exclude module: 'hamcrest-core'
}
androidTestCompile('org.robolectric:robolectric:2.4') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-provider-api'
}
androidTestCompile 'com.android.support:support-v4:21.0.3'
}
and all the Robolectric tests are in src/androidTest/java/my/package/*Test.java
. This has all been working perfectly. I can run the tests as part of a normal Gradle build, or through IntelliJ's JUnit GUI.
Now I need to add some tests that can't use Robolectric and need to run on a real Android device. Given that I've already had to use the androidTest
variant for my Robolectric tests, how can I add my instrumented tests to this project, and still allow the Robolectric tests to run without a device?
This is super outdated setup, I suggest:
Use latest stable android gradle plugin:
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
}
}
Remove Robolectric gradle plugin:
//apply plugin: 'robolectric'
//robolectric {
// include '**/*Test.class'
//}
Move test sources to test
folder
androidTestCompile
to testCompile
Now you can run your tests with ./gradlew test<Flavour>DebugUnitTest
. And you can add your instrumental tests to androidTest
folder.
Consider also upgrade Robolectric to 3.1
version