6

I am using the robolectric gradle plugin to write my unit tests for Android. Everything works fine so far, apart from being able to properly debug my tests using Android Studio.

I did some investigation (http://forums.gradle.org/gradle/topics/how_do_you_attach_a_debugger_to_gradle_so_that_i_can_debug_it_running_a_task) and what I ended up is:

  1. Start debugable gradle config from console gradlew -DtestDebug.debug=true app:clean app:testDebug This will stop gradle build and wait for listener at 5005

  2. Create "Remote" launch configuration in Android Studio, which will attach on port 5005

  3. Launch that configuration from Android Studio in Debug-mode

Step 1 + 3 have to be done each time, i want to debug my tests. That is pretty annoying. I would like to have a way, that I can directly launch a gradle build from intelliJ, which will automatically attach the debugger. How can I do that?

Info: (Of course, the ideal solution would be that I can launch unit tests directly via the JUnit-configuration of IntelliJ, since that would give me IDE support, neat display of failing and passing tests, etc. But I experienced problems with the JUnit builds not finding the Manifest file, etc, so I gave up on that for now.)

**Gradle build file **

apply plugin: 'com.android.application'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+'
    }
}

apply plugin: 'robolectric'

android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
    }

    defaultConfig {
        applicationId "test.fs.test"
        minSdkVersion 14
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-core:1.3'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'

    androidTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile('org.robolectric:robolectric:2.3') {
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'
    }
}

apply plugin: 'idea'

idea {
    module {
        testOutputDir = file('build/test-classes/debug')
    }
}
Flo
  • 1,469
  • 1
  • 18
  • 27
  • 1
    Here is my explanation how to run robolectric test in AS: http://stackoverflow.com/questions/19891564/how-to-run-unit-tests-with-android-studio – Eugen Martynov Aug 24 '14 at 09:53
  • Awesome, changing the working directory to the folder where the AndroidManifest resides in did the trick! I will be doing a few more tests to see how this works when including other project dependencies or merging manifests, etc. One thing I noticed though is that AS does not seem to compile my test files properly. I ended up executing `gradlew app:testDebug` once (to compile my tests) before being able to debug them via AS. But even if that requires 2 steps, that's still a lot easier than my previous solution. – Flo Aug 24 '14 at 14:19
  • I now also tried to work with library projects and (although it requires quite some effort to configure it), I finally got it running, see: http://stackoverflow.com/a/25473702/1406325 - That solution does not require the workspace to be set to the manifest folder, as it can be configured via the `org.robolectric.Config.properties` file... – Flo Aug 24 '14 at 16:20

0 Answers0