Confused about testCompile and androidTestCompile in Android Gradle
Simply testCompile
is the configuration for unit tests (those located in src/test) and androidTestCompile
is used for the test api (that located in src/androidTest). Since you are intending to write unit tests, you should use testCompile
.
Update: The main distinction between the two is the test
sourceset runs in a regular Java JVM, whereas the androidTest
sourceset tests run on an Android device (or an emulator).
Differences between testCompile and androidTestCompile for junit imports
There's a great answer here explaining the difference:
Simply
testCompile
is the configuration for unit tests (located insrc/test
) andandroidTestCompile
is used for the test API (located insrc/androidTest
)....
The main distinction between the two is the
test
sourceset runs in a regular Java JVM, whereas theandroidTest
sourceset tests run on an Android device (or an emulator).
unsupported gradle method instrumentTestCompile
I found the response here:
http://android.amberfog.com/?p=894
instrumentTestCompile()
was renamed to androidTestCompile()
in the latest version of gradle plugin.
Don't fail the gradle build if a test is failing with the gradle-android-test-plugin
The correct syntax with AndroidConnectedTests is as following:
project.gradle.taskGraph.whenReady {
connectedAndroidTest {
ignoreFailures = true
}
}
Or for newer Gradle-plugin (1.3.0 and later), try:
project.gradle.taskGraph.whenReady {
connectedDebugAndroidTest {
ignoreFailures = true
}
}
But if you have Flavors (in newer Gradle versions):
android {
// ...
project.gradle.taskGraph.whenReady {
android.productFlavors.all { flavor ->
// Capitalize (as Gralde is case-sensitive).
def flavorName = flavor.name.substring(0, 1).toUpperCase() + flavor.name.substring(1)
// At last, configure.
"connected${flavorName}DebugAndroidTest" {
ignoreFailures = true
}
}
}
}
Now the test task is not failing the build anymore, which ensures the coverage report is created, and you can pick up the failed tests with your build server to mark the build as unstable etc.
Robolectric - AndroidStudio - Could not find method testCompile() for arguments
SourceSets:
I can tell you are coming from an Eclipse-based project based on your sourceSets
:
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/java']
aidl.srcDirs = ['src/main/java']
renderscript.srcDirs = ['src/main/java']
res.srcDirs = ['src/main/res']
assets.srcDirs = ['src/main/assets']
}
}
I would suggest removing your sourceSets
and using the Android Studio/Intellij project structure: src/main, src/test
.
Running Unit
tests from the IDE
:
Make sure you have "Unit Tests" turned on.
This allows you to run your unit tests in the IDE. Please following the instructions from the official Android documentation:
Source: http://tools.android.com/tech-docs/unit-testing-support
Running Unit
tests from commandline:
gradlew test
Related Topics
How to Get The Dimensions of a Drawable in an Imageview
How to Use a Library Project in Android Studio
Google Maps Android API V2 - Sample Code Crashes
Android Build Gradle Is Too Slow (Dependency Resolution)
How to Find The Amount of Free Storage (Disk Space) Left on Android
Android: How to Get a Modal Dialog or Similar Modal Behavior
Designing Android Apps for Tablets
Android: Radio Button in Custom List View
Android 4.4.2 - Java.Lang.Runtimeexception: Performing Stop of Activity That Is Not Resumed
How to Look-Up a Contact's Name from Their Phone Number on Android
How to Hide The Soft-Key Bar on Android Phone
Firebase Console: How to Specify Click_Action for Notifications
How to Embed Vlc Media Player to My Android App
Change Navigation Bar Icon Color on Android
How to Change The Overscroll Color in Android 2.3.1
Does Android Studio Have a Hierarchy Viewer or Layout Inspector