Robolectric is pretty much the de facto unit test framework for Android nowadays. It’s great, it’s fast (much faster than the out-of-the-box Android instrumentation test), and it gives you back joy of writing unit tests.
So you enjoy it so much that over the time you add more and more tests to your test suite that it no longer feels that fast anymore everytime you rerun it for verification? Well, it still needs to launch a VM to execute your tests after all. To improve this, you can make use of Robolectric’s Gradle plugin configuration to filter only tests that need to be rerun.
Now when you run your Gradle test command, throw in a
-PtestFilter=<filter> command line parameter to instruct Robolectric to run only the tests specified by your filter:
The above command will execute all tests with class name following
*Activity*Test pattern. No parameter will default to running all tests. A drawback I found with this approach is that if your pattern has no matches, Robolectric executes all tests instead of no test (since there are none).
Update (Mar 21st, 2015)
Latest Robolectric Gradle plugin with support for Android Gradle plugin 1.1.0 allows one to filter tests by running
./gradlew testDebug --tests='*.MyTestClass'
For more information, see Unit testing docs for Android Gradle plugin.
Another tip to speed up your test run is to tell Robolectric to parallelize your test run using more processes if possible: