android difference between testImplementation and androidTestImplementation in build.gradle

AndroidTestingGradleDependencies

Android Problem Overview


I am using andorid studio 3.2 and I am new to testing. I want to add a testing library but I am not getting what should i write in the dependencies section testImplentation or androidTestImplementation. What is the difference between these two?

Android Solutions


Solution 1 - Android

If you create android project using Android Studio. You can find three directories like following. (each directory called Source Set)

  • app/src/main
  • app/src/androidTest : for android UI test. It needs virtual or real device. (Espresso, UI automator)
  • app/src/test : for android independent test.

The androidTest directory contains test cases that touch the screen or check what is displayed on the screen. In the test directory, we mainly do unit tests such as testing one function.

But test directory is not only for Unit test. You can also write Integration test like HTTP call. Even you can UI test in test directory using Robolectric library.(It's really fast rather than Espresso)

So what is testImplementation and androidTestImplementation? (each called Configuration)

  • testImplementation : adds dependency for test source set
  • androidTestImplementation : adds dependency for androidTest source set

See this articles for details.

Solution 2 - Android

>implementation—The dependency is available in all source sets, including the test source sets.

>testImplementation—The dependency is only available in the test source set.

>androidTestImplementation—The dependency is only available in the androidTest source set.

Android source sets are :

>main: Contains your app code. This code is shared amongst all different versions of the app you can build (known as build variants)

>androidTest: Contains tests known as instrumented tests.

>test: Contains tests known as local tests.

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
Questionsrb1991View Question on Stackoverflow
Solution 1 - AndroidgalcyurioView Answer on Stackoverflow
Solution 2 - AndroidCoolView Answer on Stackoverflow