Why isn't Gradle running my unit tests?

I’m in the middle of migrating a project from Intellij IDEA 12 to Android Studio, and we’re trying to set up the Gradle build files to also let us build our project on a continuous integration server. I’m having a lot of trouble migrating the unit tests though.

My unit tests use Robolectric. I’m using the gradle-android-test-plugin to get Robolectric working. My unit tests are under src/test/java, next to the main source files in src/main/java. I have four subprojects included in the root’s settings.gradle and an empty root build.gradle.

The unit tests only run for one of my subprojects when I execute “gradle test”. For all of the other subprojects, the test classes are built and I see them under build/test-classes, but none of the tests are executed.

What could I be missing? I’m totally new to Gradle (and other Java build automation tools), so please let me know if I should be posting any more information about my configuration. Thanks!

We’d need more information. For starters, how and where have you configured test execution?

I’m using the (default?) “test” task. I’m not sure whether this is built-in to Gradle, or comes from the “android”, “android-library”, or “android-test” plugin. Where should I look to find out more information for you?

I have my test classes under the $SUBPROJECT/src/test/java directory, and they’re configured with JUnit 4’s annotations.

I have the same build.gradle file for both the subproject with working tests and the subproject that doesn’t run any tests:

buildscript {
    repositories {
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
        classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT'
    }
}
apply plugin: 'android-library'
apply plugin: 'android-test'
  repositories {
    mavenCentral()
}
  android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"
      defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
    }
    release {
        runProguard false
        proguardFile 'proguard-rules.txt'
        proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
    }
}
  dependencies {
 compile 'com.android.support:appcompat-v7:+'
      testCompile 'junit:junit:4.11'
    testCompile 'org.robolectric:robolectric:2.2'
    testCompile 'org.mockito:mockito-core:1.9.5'
}
  task showTestResults(type:Exec, dependsOn:test) {
    // TODO: Fix this to read the test reports directory from the build system
    workingDir 'build/test-report'
    commandLine 'open', 'index.html'
}

This is all done by the ‘android-test’ plugin. You’ll get the best result by asking the author of that.