A fresh Android project with tests already set up.
- Loads nicely in Android Studio
- Robolectric tests working and debuggable in IDE (for unit testing)
- Espresso tests working and debuggable in IDE (for functional testing)
- Latest Lollipop compat libs ready to use
- Test coverage enabled
- Android 21 (5.0 Lollipop), with compatiblity back to 10 (2.3.3 Gingerbread)
- Android Studio 1.0
- Gradle 2.2
- Build Tools 21.1.2
- Appcompat-v7 21.0.3
- Dagger for dependency injections
- Butter Knife for view injections
-
Open Settings, Plugins, Browse Repositories and install Android Studio Unit Test
-
Import the project as a Gradle project. This can take a while. Specially downloading 30 megs of Gradle binaries usually takes 30 minutes (Gradle, CDN, please!). Once that's done, it downloads all the deps on first run. This is usually faster.
-
Open Run Configurations, click on Defaults, Android Tests and put
android.support.test.runner.AndroidJUnitRunner
as the instrumentation runner (pro tip: click browse and type ajr). This makes Android Studio use the Espresso runner instead of the default one. -
For the Robolectric tests, create a new JUnit configuration, name it Robolectric and put the following:
- Test kind: All in package
- Package:
com.example.tests.unit
- Search for tests: In single module
- Working directory:
$MODULE_DIR$
Also create a new Gradle Configuration that runs
testDebug
and reference it under the Before launch section (also atestClasses
is automatically added by the Android Studio plugin, but it only compiles test classes and not the resources).
You should now be able to run tests by right-clicking on the test method. Be sure to choose JUnit for the Robolectric tests and Android test for the Espresso tests. You can also run the tests from the console.
For Robolectric/unit tests, type:
./gradlew test
Or, if you want to generate a test coverage report:
./gradlew jacocoTestReport
For Espresso/instrumentation tests, type (at least one device/emulator should be connected via ADB):
./gradlew connectedAndroidTest
-
Make sure you use Java 1.7 (x64), and that your
JAVA_HOME
is set to that folder. I had problems with Java 8. -
If Android Studio doesn't put your Robolectric tests on a greenish background in the project view, that means it didn't recognize it as test source and you can't select the right package when creating the JUnit run configuration. This can happen if:
-
Also if it complains about JUnit 3.8, that means that the class path is screwed up (JUnit 4.11 should have come first), something the plugin above is supposed to take care of.
-
If you're getting an exception about your test class not being found, verify it's a Robolectric tests class and not an Espresso test. Fix the package of the JUnit run configuration if that's the case.
Obviously stuff didn't work out of the box (hence this project). Explanations see below.
Since Robolectric is not part of the official tool chain, a Gradle plugin is needed in order to make it part of the build. There are several:
- robolectric/robolectric-gradle-plugin - A plugin by the Robolectric team.
- novoda/gradle-android-test-plugin
- JCAndKSolutions/android-unit-test - The one used by this seed.
The problem
with the offical plugin is that it uses the existing androidTest
source
folder, which makes separation between unit tests and functional tests
impossible. The last plugin has the advantage of better Android Studio
Integration as well.
Note that Robolectric is currently broken due to issues with appcompat-v7.
The Espresso stack has a few advantages over other tools (Robotium, Selendroid, UIAutomator...), namely:
- Gradle support
- Android Studio support
- Works well with Spoon
- Fluent API
- Well synchronized with app's UI thread (no
Thread.sleep()
necessary)
Espresso is now officially part of Google's tool chain.
- almozavr/deckard-gradle (a fork
from robolectric/deckard-gradle) - Main inspiration for this project. This
fork replaced the official
robolectric-gradle-plugin
byandroid-unit-test
from JCAndKSolutions.