How to set up unit tests for Android / Gradle -
after having read android guide on testing android gradle plugin, wanted set junit tests pojo's don't run instrumented tests. idea tests code doesn't depend on android should fast (and facilitate tdd).
is there standard way set source set , task in build.gradle accomplish this? main question, secondary question what's wrong attempt below...
i'm using android studio 0.4.2 , gradle 1.9, experimenting simple junit test class in new "test" folder. here have far, when run "gradle testpojo" result:
:android:assemble up-to-date :android:compileunittestjava up-to-date :android:processunittestresources up-to-date :android:unittestclasses up-to-date :android:testpojo failed * went wrong: execution failed task ':android:testpojo'. > failed read class file /path/to/project/android-app/build/classes/unittest/testclass.class
i verified class in fact there, i'm confused why task not able read file.
here build.gradle file:
... sourcesets { unittest { java.srcdir file('src/test/java') resources.srcdir file('src/test/resources') } } dependencies { ... unittestcompile 'junit:junit:4.11' } configurations { unittestcompile.extendsfrom instrumenttestcompile unittestruntime.extendsfrom instrumenttestruntime } task testpojo(type: test, dependson: assemble){ description = "run pojo unit tests (located in src/test/java...)." testclassesdir = sourcesets.unittest.output.classesdir android.sourcesets.main.java.srcdirs.each { dir -> def builddir = dir.getabsolutepath().split('/') builddir = (builddir[0..(builddir.length - 4)] + ['build', 'classes', 'debug']).join('/') sourcesets.unittest.compileclasspath += files(builddir) sourcesets.unittest.runtimeclasspath += files(builddir) } classpath = sourcesets.unittest.runtimeclasspath } check.dependson testpojo
i'd suggest use robolectric
, declare this:
classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
and apply plugin:
apply plugin: 'robolectric'
Comments
Post a Comment