Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about testCompile and androidTestCompile in Android Gradle

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?

like image 934
Lucas Avatar asked Mar 12 '15 22:03

Lucas


People also ask

What is the difference between compile and testCompile Gradle?

compile is the group of dependencies you need to build your application while testCompile is a group of dependencies that you need only for testing. This specifies that hibernate-core is needed to build your code but junit (a testing framework) is needed just for testing.

Why there are two Gradle files in Android Studio?

Android Studio projects contain a top-level project Gradle build file that allows you to add the configuration options common to all application modules in the project. Each application module also has its own build. gradle file for build settings specific to that module.

What is @AAR in Gradle?

The AAR format. is the binary distribution of an Android Library Project. As described here in the official Android Tools documentation. In your case, when adding a compile dependency in an Android Gradle project, adding "@aar" means that you would like to fetch the @aar file and not a regular JAR file.

What is the difference between compile and implementation in Gradle?

Fortunately, the implementation dependency configuration provides the same functionality as compile. You should always use implementation rather than compile for dependencies, as compile is now deprecated or removed in the case of Gradle 7+.


2 Answers

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

like image 92
Mark Vieira Avatar answered Sep 28 '22 01:09

Mark Vieira


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

like image 26
Amit Kaushik Avatar answered Sep 28 '22 00:09

Amit Kaushik