Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android unit test support does not work in android library modules

I am writing junit tests on android project using the new unit test support http://tools.android.com/tech-docs/unit-testing-support.

While the unit tests run on the 'com.android.application' module perfectly but they always fail on the 'com.android.library' modules. This has not been documented in http://tools.android.com/tech-docs/unit-testing-support . So I wonder whether I am the culprit.

When I write those tests on library modules, the tests can not find the classes on the module and always gives following errors:

package does not exist

error: cannot find symbol

The android unit test support is in experimental phase right now, but is there a solution to it.

UPDATE

I have added this issue to android issue tracker https://code.google.com/p/android/issues/detail?id=161038

like image 549
Gaurav Vashisth Avatar asked Mar 20 '15 09:03

Gaurav Vashisth


2 Answers

It looks like the task to compile the unit tests doesn't depend on the task to compile the library code. The following fixed it for me:

afterEvaluate {
    tasks['assembleDebugUnitTest'].dependsOn(tasks['assembleDebug'])
}

I run the tests using

./gradlew testDebug

If you don't want to modify your build.gradle, manually specify the assembleDebug task on the command line should also do the trick:

./gradlew assembleDebug testDebug
like image 149
cketti Avatar answered Oct 12 '22 09:10

cketti


In my android library project I also failed to get the tests running. What I did was create a test application that uses the library and wrote tests in the application that call the library methods.

This might not be the ideal solution, but was the way we got this to work.

like image 31
Fred Avatar answered Oct 12 '22 11:10

Fred