1

我有一个使用 Robolectric 进行测试的 Android 项目。build.gradle 的相关部分看起来有点像这样:

apply plugin: 'robolectric'

robolectric {
    include '**/*Test.class'
}

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

    androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
    androidTestCompile('junit:junit:4.12') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile('org.robolectric:robolectric:2.4') {
        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'
    }
    androidTestCompile 'com.android.support:support-v4:21.0.3'
}

并且所有的 Robolectric 测试都在src/androidTest/java/my/package/*Test.java. 这一切都运作良好。我可以将测试作为普通 Gradle 构建的一部分运行,也可以通过 IntelliJ 的 JUnit GUI 运行。

现在我需要添加一些不能使用 Robolectric 并且需要在真正的 Android 设备上运行的测试。鉴于我已经不得不androidTest为我的 Robolectric 测试使用该变体,我如何将我的仪器测试添加到这个项目中,并且仍然允许 Robolectric 测试在没有设备的情况下运行?

4

1 回答 1

1

这是超级过时的设置,我建议:

  1. 使用最新的稳定android gradle 插件:

    buildscript {
      dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
      }
    }
    
  2. 删除Robolectric gradle 插件:

    //apply plugin: 'robolectric'
    
    //robolectric {
    //    include '**/*Test.class'
    //}
    
  3. 将测试源移动到test文件夹

  4. 更改androidTestCompiletestCompile

现在您可以使用./gradlew test<Flavour>DebugUnitTest. 您可以将仪器测试添加到androidTest文件夹中。

还考虑将Robolectric升级到3.1版本

于 2016-06-16T13:38:07.010 回答