How to set up unit tests for Android / Gradle

In a recent post we found that maven and gradle are designed for unit testing but not for integration testing (although gradle makes it fairly easy to write separate integration tests).

In Android, it turns out that there is an extensive testing system in place, but that it is not for unit testing, it is for integration testing (actually running your complete application on connected devices). Hilariously, Android seems to have the inverse problem of what we saw in maven and gradle: integration tests are baked in to the system, and unit testing requires special work.

To this end, after having read the android guide on testing with the android gradle plugin, I wanted to set up JUnit tests for my POJO’s that don’t run with the instrumented tests. The idea was that tests for code that doesn’t depend on Android should be very fast (and facilitate TDD).

Unfortunately, after much googling and experimentation, I was forced to the conclusion that simple unit testing is just not supported out of the box with android and gradle. Indeed I was not able to make it work the way I’d like. The best solution I can come up with for the time being is that any code that is completely independent of android is best placed in its own project (say, a sibling to the android application in the gradle main project) and run unit tests there.

If anybody reads this and has ideas, feel free to drop an answer on StackOverflow.

Advertisements

Leave a comment

Filed under Android, Software Engineering

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s