使用IntelliJ/AndroidStudio调试基于gradle的单元测试

Flo*_*Flo 6 debugging android intellij-idea gradle android-gradle-plugin

我正在使用robolectric gradle插件为Android编写单元测试.除了能够使用Android Studio正确调试我的测试之外,到目前为止一切正常.

我做了一些调查(http://forums.gradle.org/gradle/topics/how_do_you_attach_a_debugger_to_gradle_so_that_i_can_debug_it_running_a_task),我最终得到的结果是:

  1. 从控制台启动可调试的gradle配置 gradlew -DtestDebug.debug=true app:clean app:testDebug 这将停止gradle构建并在5005等待监听器

  2. 在Android Studio中创建"远程"启动配置,该配置将附加在端口5005上

  3. 在Debug-mode中从Android Studio启动该配置

每次都要完成步骤1 + 3,我想调试我的测试.这很烦人.我想有办法,我可以直接从intelliJ启动gradle构建,它会自动附加调试器.我怎样才能做到这一点?

信息:(当然,理想的解决方案是我可以通过IntelliJ的JUnit配置直接启动单元测试,因为这样可以给我IDE支持,整齐地显示失败和通过测试等等.但是我遇到了问题. JUnit构建时没有找到Manifest文件等,所以我暂时放弃了它.)

**Gradle构建文件**

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')
    }
}
Run Code Online (Sandbox Code Playgroud)