Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an Android Test Module in IntelliJ 13 for a Gradle Android project?

I have successfully create a Android project in IntelliJ 13 and I want to setup the Android Testing Framework. I used the new project wizard to create the android project using Gradle. When I go to add a new module I only have options for "Gradle: Android Module" and "Gradle: Java Library", the "Test Module" option is missing.

How do I generate an Android Test Module? I have read http://www.jetbrains.com/idea/webhelp/testing-android-applications.html but I can never find any "test" option.

If an Android Test Module can not be automatically generated, then how do I configure and use the Android Testing Framework with a Gradle Android Project? Links to examples or documentation is very much appreciated.

Details: IntelliJ 13.1.3

like image 831
Mike Rylander Avatar asked Jul 18 '14 18:07

Mike Rylander


People also ask

How do I create a test package in IntelliJ?

Create a new test class manuallyRight-click the test root folder or package in the test root folder in which you want to create a new test and select New | Java Class. Name the new class and press Enter . Press Alt+Insert and select Test Method to generate a new test method for this class.

How do you run a test case using gradle command?

Use the command ./gradlew test to run all tests.

How do I specify a module in IntelliJ?

Set up a module SDKFrom the main menu, select File | Project Structure | Project Settings | Modules. Select the module for which you want to set an SDK and click Dependencies. If the necessary SDK is already defined in IntelliJ IDEA, select it from the Module SDK list.


1 Answers

So currently your directory structure should look something like this:

ProjectDirectory/
  res/
  src/
    main/
      java/
        your.package.name
          MyClass.java

All you need to do is add a src directory for androidTest:

ProjectDirectory/
  res/
  src/
    main/
      java/
        your.package.name
          MyClass.java
    androidTest/
      java/
        your.package.name
          MyClassTest.java

and then resync your Gradle file with your project, and it should be detected as a test directory. Then you can run the connectedCheck or connectedAndroidTest tasks (I'm unclear on the difference in the two) to run the tests.

If your directory structure differs from the above (if, for instance, you important an Eclipse-style project), you can specify the alternate src directory in your build.gradle script:

android {
    sourceSets {
        androidTest {
            java.srcDirs = ['path/to/test/src']
        }
    }
}
like image 69
Kevin Coppock Avatar answered Oct 05 '22 22:10

Kevin Coppock