Confused About Testcompile and Androidtestcompile in Android Gradle

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 in src/test) and androidTestCompile is used for the test API (located in src/androidTest).

...

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).

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.

Unit testing in Android Studio

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



Leave a reply



Submit