Debug gradle-based unit tests with IntelliJ / Andr

2019-04-27 12:21发布

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')
    }
}

0条回答
登录 后发表回答