Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure test folder for unit testing in Android studio

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?

like image 902
Ali Avatar asked Jun 29 '15 13:06

Ali


2 Answers

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

like image 99
Eugen Martynov Avatar answered Sep 28 '22 00:09

Eugen Martynov


Almost fine your Gradle script but try do that:

sourceSets {
    androidTest.setRoot('src/test')
    androidTest {
        java.srcDirs = ['src/test/java']
    }
}
like image 22
Ciro Rizzo Avatar answered Sep 27 '22 23:09

Ciro Rizzo