8
votes

I have added a folder for unit testing in my android studio project. The default folder is andoidTest but I have added a new folder and name in test instead. (like robolectric sample tests)

When I add test Dependency in my build.gradle under module such as

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

They do not get added to external libraries under project, but when I use the default configuration and use androidTestCompile, it can added external libraries.

Then I thought that maybe I should setRoot for tests in gradle, so I used following in android tag in build.gradle:

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

But still problem remained. I can run tests using gradlew, but imports in classes in test folder do not apply as well as no external library for test purpose is visible.

Anyone have any solution for this issue?

2
You should change in build variants tool window test artifact value from "Andrio tests" to "unit tests"Eugen Martynov
And I think this question is already duplicate :)Eugen Martynov
Thanks a lot @Eugen. If you would like You can share your answer so I can accept. And tell me where is has been asked before.Ali
That is test directory. What about androidTest directory?IgorGanapolsky

2 Answers

7
votes

I was searching and didn't find answer that I thought already covered this. So decided to create new one for the future.

Answer Android Studio is not picking up unit tests automatically right now. I know it is planned for 1.3 version.

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

7
votes

Almost fine your Gradle script but try do that:

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