Setting up a test folder for unit testing in Android studio

I added a unit test folder in my Android studio project. The default folder is andoidTest, but instead I added a new folder and name. ( as tests of robolectric samples )

When I add Test Dependency to my build.gradle in a module like

testCompile("junit:junit:${junitVersion}") testCompile ("org.robolectric:robolectric:${robolectricVersion}") 

They are not added to external libraries in the project, but when I use the default configuration and use androidTestCompile , it can add external libraries.

Then I thought that maybe I need to install Root for tests in gradle, so I used the following code in the android tag in the build.gradle file:

 sourceSets { androidTest.setRoot('src/test') } 

But the problem still remained. I can run tests using gradlew , but import into classes in the test folder is not applied, since the external library is not visible for testing purposes.

Does anyone have a solution for this problem?

+8
android android-gradle unit-testing gradle robolectric
source share
2 answers

I searched and did not find an answer which, as I thought, already covered this. Therefore, we decided to create a new one for the future.

Answer Android Studio is not collecting unit tests automatically right now. I know this is planned for version 1.3.

So, you need to change the value of the test artifact from Android Instrumental Tests to Unit Tests in the Build Variants tool window: enter image description here

+6
source share

Practice terminating your Gradle script, but try doing this:

 sourceSets { androidTest.setRoot('src/test') androidTest { java.srcDirs = ['src/test/java'] } } 
+7
source share

All Articles