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?
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.
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.
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.
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+.
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).
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With