我有一个使用 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 设备上运行的测试。鉴于我已经不得不为我的 Robolectric 测试使用 androidTest
变体,我如何才能将我的插桩测试添加到该项目,并且仍然允许 Robolectric 测试在没有设备的情况下运行?
最佳答案
这是 super 过时的设置,我建议:
使用最新稳定的 android gradle 插件:
buildscript { dependencies { classpath 'com.android.tools.build:gradle:2.1.0' } }
删除 Robolectric gradle 插件:
//apply plugin: 'robolectric' //robolectric { // include '**/*Test.class' //}
将测试源移动到
test
文件夹- 更改
androidTestCompile
至testCompile
现在您可以使用 ./gradlew test<Flavour>DebugUnitTest
运行测试.您可以将仪器测试添加到 androidTest
文件夹。
同时考虑将 Robolectric 升级到 3.1
版本
关于android - 使用 Robolectric 测试将设备上的仪器化测试添加到项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37859637/