Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle, Robolectric, and Espresso

Has anyone successfully gotten Robolectric and Espresso working (together) when building with Gradle (Android Studio)?

I've gotten Robolectric working in Android Studio due largely to Peter Friese's post http://www.peterfriese.de/android-testing-with-robolectric/, but I'm unsure of how to integrate Espresso due to instrumentRunner/instrumentTest collisions.

Ideally I'd have a directory structure like the following:

 |--src
    └── main (application source - exists)
    └── test (Robolectric unit tests go here - exists)
    └── testEspresso [*new*] (espresso tests go here)

My Gradle+Android knowledge isn't very extensive, and I'm unsure of whether this is really a feasible thing (time, complexity, and fragility levels aren't too extreme) given the current state of Gradle+Android and if it is, how to go about doing so. Also in case it's of relevance, I have 2 different build flavors.

like image 767
loeschg Avatar asked Dec 02 '13 22:12

loeschg


People also ask

What is the difference between Mockito and Robolectric?

Mockito is used for mocking the dependency which means if you want to access an real object in test environment then you need to fake it or we can say mock it. Now a days it is very easier to do mocking of the objects with Mockito. Roboelectric is the industry-standard unit testing framework for Android.

What is Robolectric?

Robolectric is a framework that allows you to write unit tests and run them on a desktop JVM while still using Android API. Robolectric provides a JVM compliant version of the android. jar file.

What are instrumented tests?

Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of Instrumentation. This class provides access to the application context and APIs to manipulate the app under test and gives instrumented tests their name.


2 Answers

For a good example of Robolectric and Espresso working together in harmony, check out this sample project created by the Robolectric team:

https://github.com/robolectric/deckard-gradle

like image 146
plackemacher Avatar answered Sep 28 '22 01:09

plackemacher


Another option, which doesn't include any "hacks, Gradle plugins, IDE plugins, or IML editing" is http://blog.blundellapps.co.uk/android-gradle-app-with-robolectric-junit-tests/. Jake Wharton recommended this approach. With this, your Robolectric tests are in a separate Java module, separate from your main project. Espresso code lives with your main project.

Using JW's double-espresso (https://github.com/JakeWharton/double-espresso) will simplify including Espresso and the dependency management madness that can come with it.

The solution @plackemacher mentioned, deckard-gradle, is still an option and is being maintained, though it does require some hacking/surgery to get working with the IDE.

Update: See the discussion here regarding Robolectric and Gradle (relates to Gradle): https://groups.google.com/forum/#!topic/robolectric/xsOpEwtdTi4/discussion

Update: While I have not used it, this looks to be the ticket. https://github.com/JakeWharton/double-espresso.


For the time being, I elected to create a separate Espresso branch (git) which has the proper testRunner and test directory set. Not a horribly elegant solution... does require some vigilance to keep everything up-to-date, but it's working for now!

Will definitely change the answer should somebody find a better solution.

like image 30
loeschg Avatar answered Sep 28 '22 01:09

loeschg