A Gradle plugin which enables Robolectric tests.
Currently compatible with version 0.13.x of the android gradle plugin.
If you are starting a new app, or want to try this plugin in the simplest possible environment, the easiest way to start is to use deckard-gradle.
deckard-gradle illustrates how to run Robolectric and Espresso tests in IntelliJ, Android Studio or the command-line.
Add the plugin to your buildscript
's dependencies
section:
classpath 'org.robolectric:robolectric-gradle-plugin:0.13.+'
Apply the robolectric
plugin:
apply plugin: 'robolectric'
Add test-only dependencies using the androidTestCompile
configuration:
androidTestCompile 'org.robolectric:robolectric:2.3'
Place your tests in src/test/java
or src/androidTest/java
You can also add per-build type and per-flavor tests by using the same folder naming conventions (e.g., src/testPaid/java
, src/testDebug/java
).
Run your tests by calling gradle clean test
.
robolectric {
// configure the set of classes for JUnit tests
include '**/*Test.class'
exclude '**/espresso/**/*.class'
// configure max heap size of the test JVM
maxHeapSize = '2048m'
// configure the test JVM arguments
jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'
// Specify max number of processes (default is 1)
maxParallelForks = 4
// Specify max number of test classes to execute in a test process
// before restarting the process (default is unlimited)
forkEvery = 150
// configure whether failing tests should fail the build
ignoreFailures true
// use afterTest to listen to the test execution results
afterTest { descriptor, result ->
println "Executing test for {$descriptor.name} with result: ${result.resultType}"
}
}
In a nutshell, you should be able to import into these IDEs (and continuously sync when you change your build.gradle).
It bears repeating, though: if you see the dreaded Stub!
exception:
!!! JUnit version 3.8 or later expected:
java.lang.RuntimeException: Stub!
at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
at junit.textui.TestRunner.<init>(TestRunner.java:54)
at junit.textui.TestRunner.<init>(TestRunner.java:48)
at junit.textui.TestRunner.<init>(TestRunner.java:41)
...you will have to hand-edit your dependencies (in the IDE for IntelliJ, or hand-editing your IML file in Studio). See deckard-gradle for details.
Copyright 2013 Square, Inc.
2014 Pivotal Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.