junit testing with gradle for an android project
Solution 1
You don't need the Java plugin, since the Android will take care of what you need mostly, from what I've seen so far.
I managed to get my Robolectric and junit tests running via this man's blog: http://tryge.com/2013/02/28/android-gradle-build/
My build.gradle file looks like this (where my test files are in the {projectdir}/test directory.
...
// Unit tests
sourceSets {
unitTest {
java.srcDir file('test')
resources.srcDir file('test/resources')
}
}
dependencies {
unitTestCompile files("$project.buildDir/classes/debug")
unitTestCompile 'junit:junit:4.11'
unitTestCompile 'org.robolectric:robolectric:2.1.1'
unitTestCompile 'com.google.android:android:4.0.1.2'
}
configurations {
unitTestCompile.extendsFrom runtime
unitTestRuntime.extendsFrom unitTestCompile
}
task unitTest(type:Test, dependsOn: assemble) {
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}
build.dependsOn unitTest
Solution 2
AndroidStudio and the new Android Gradle plugin are now offering official unit test support.
This is supported from Android Studio 1.1+ and Android Gradle plugin version 1.1.0+
Dependencies can now be declared as testCompile:
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
More details here: Unit testing support - Android Tools Project Site.
Solution 3
This guide might help - http://www.slideshare.net/tobiaspreuss/how-to-setup-unit-testing-in-android-studio
Latest gradle the test should be under androidTest dir
Also in your gradle.build:
dependencies {
androidTestCompile 'junit:junit:4.+'
}
also add those under defaultConfig {
testPackageName "test.java.foo"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
Related videos on Youtube
Comments
-
ligi almost 4 years
I am trying to get tests ( junit and robolectric ) working in an Android project but am totally stuck. My main problem is that all testing I found with gradle somehow pull in the java plugin and then I get this error:
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
The only way out I see at the moment is to split into test and app project - but I would like to avoid that. Any examples/hints would be highly appreciated!
In the official documentation there is no mention of unit-testing - only Instrumentation-Tests - but I want unit-tests to get results fast.
-
Jose Alcérreca about 9 yearsNote that there is official unit testing support since AS 1.1 and Android Gradle plugin 1.1.0. tools.android.com/tech-docs/unit-testing-support
-
-
ligi over 10 yearsthanks! does that work in combination with Android-Studio for you? Would love to run the tests from the IDE but that is not yet working for me
-
mmm111mmm over 10 yearsI don't use Android Studio yet. I simply run 'gradle build' from the command line. Since AS has Gradle integration, and it runs 'build', it will also run the 'unitTest' task, so I would have thought it would be run, although I haven't tested this.
-
gleenn over 10 yearsI can't seem to get this to work, I get classpath errors where my unit test can't find robolectric or junit. I'm running Gradle 1.6 from the command line with the task ./gradlew unitTest
-
Karim Varela about 10 yearsI also can't get this to work. I get "package android.test does not exist"
-
Nilzor almost 10 yearsIs this supposed to run the unit tests as well as compile?
-
Chris almost 10 yearsSince Anroid Build Tools v0.11 the path for the class files changed.
unitTestCompile files("$project.buildDir/intermediates/classes/release")
-
fncomp almost 10 years@Chris Should use debug folder instead:
unitTestCompile files("$buildDir/intermediates/classes/debug")
, but good to point that out. -
inder almost 10 yearsI am using API 18, so com.google.android:android dependency won't work for me. Anyone knows how to refer to the Android SDK instead?
-
babay over 8 yearsNope. If your unit test has no dependencies or only has simple dependencies on Android, you should run your test on a local development machine. developer.android.com/intl/ru/training/testing/unit-testing/…