Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure android tests directory?

The Android gradle build forces my AndroidTests to be in src/androidTests. How do I change this to be another directory of my choosing?

Here is some background: I am migrating a project from eclipse.

According to the build documentation, when I add this to my gradle build file:

androidTest.setRoot('tests')

The android gradle build does this: "Note: setRoot() moves the whole sourceSet (and its sub folders) to a new folder. This moves src/androidTest/* to tests/*"

That is great for the build and makes it run just fine.

However, it forces my project structure to put my androidTests within: src/AndroidTest. This makes Eclipse unable to compile because it thinks all the test code should be in a package called androidTests.com.etc.etc.etc...

I'd like to set some property like this:

androidTest
{
    java.srcDirs = ['tests']
}
like image 780
gregm Avatar asked Mar 19 '14 13:03

gregm


People also ask

When should you use the Android test directory?

A typical project in Android Studio contains two directories in which you place tests. The androidTest directory should contain the tests that run on real or virtual devices. Such tests include integration tests, end-to-end tests, and other tests where the JVM alone cannot validate your app's functionality.

Which directory is used to store unit tests in your Android project?

By default, the source files for local unit tests are placed in module-name/src/test/ . This directory already exists when you create a new project using Android Studio.

In which directory does Android Studio Place UI tests Why do they need to be there?

By default, Android Studio provides a source code directory at src/androidTest/java/ to place your instrumented and UI tests.


2 Answers

androidTest.setRoot('tests') will reset the root of the sourceSets so that

  • java is under tests/java
  • android res are under tests/res
  • etc...

You can configure those separately though with

android {
  sourceSets {
    androidTest {
      java.srcDirs = ['tests']
    }
  }
}

Note: this uses android.sourceSets because the Android plugin uses custom sourceSets.

like image 181
Xavier Ducrohet Avatar answered Oct 21 '22 04:10

Xavier Ducrohet


There are slight variations on how to do this. Putting the following in my build.gradle file worked for me:

android {
    sourceSets {
        androidTest{
            java.srcDir file('src/tests/java')
        }
    }
}
like image 36
Maxwell Avatar answered Oct 21 '22 05:10

Maxwell