Confused about testCompile and androidTestCompile in Android Gradle

AndroidGradleAndroid Gradle-PluginRobolectricAndroid Testing

Android Problem Overview


I'm new to testing world and even more to Android testing world. While doing research on Robolectric that aids with tests on android one thing confuses me the most. Sometimes on the web I see people using testCompile keyword in dependencies of the gradle build script when referencing Robolectric while others use androidTestCompile. Certainly both can't be valid?

Can somebody explain the difference between the both and which of these should be the one used when using Robolectric?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

To answer your question - Use testCompile for robolectric

why, because robolectric runs on the JVM mocking all the android device behaviour.

testCompile and androidTestCompile are "by convention" android folders which gradle uses while running tasks provided by android plugin.

androidTestDebug picks tests from androidTest folder, testDebug picks tests from test folder,

Again these are only by convention folders you can give source sets for these configurations

Note: espresso is such an awesome library try to move away from robolectric :)

Solution 3 - Android

//unit testing

testCompile 'junit:junit:4.12'

The above code is a dependency of JUnit 4 in build.gradle file in android studio. You see that it has testCompile, beacuse JUnit runs on JVM and does not require a device or emulator to run. That also means that JUnit tests will not require the application context to run and if they require we would need to "MOCK" them.

//Insturmented Unit Testing

androidTestCompile('com.android.support.test:runner:0.5', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

Now we see androidTestCompile here, because this time we intend to use the device or emulator for tests, that is Instrumentation testing. For beter clarification I would suggest to read from developer.android.com

Solution 4 - Android

To add Dependency for JVM testing or Unit testing (testing those rely only on java environment, we don’t need any android environment).

We Use testCompile directive. Example:

dependencies {
    testCompile gradleTestKit()
}

To add Dependency for Instrumentation test (Those testing mainly rely on Android environment), we use the androidTestCompile directive.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLucasView Question on Stackoverflow
Solution 1 - AndroidMark VieiraView Answer on Stackoverflow
Solution 2 - AndroidAmit KaushikView Answer on Stackoverflow
Solution 3 - AndroidLazycoder_007View Answer on Stackoverflow
Solution 4 - AndroidSubhasish NathView Answer on Stackoverflow