我试图通过仪器测试和Robolectric进行单元测试来运行Espresso(使用Double Espresso)。到目前为止我所拥有的主要是基于deckard-gradle的例子。
注意: Gradle 1.10
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.4'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.0'
}
}
apply plugin: 'android'
apply plugin: 'android-test'
android {
compileSdkVersion 19
buildToolsVersion '19.0.3'
defaultConfig {
packageName = 'com.example.app'
minSdkVersion 9
targetSdkVersion 19
versionCode 1
versionName '1.0.0'
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
buildTypes {
debug {
debuggable = true
runProguard = false
}
release {
debuggable = false
runProguard = true
}
}
sourceSets {
androidTest {
setRoot('src/test')
}
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
androidTest {
include '**/*Test.class'
exclude '**/espresso/**/*.class'
maxHeapSize = "2048m"
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:19.1.0'
androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3')
androidTestCompile('com.jakewharton.espresso:espresso-support-v4:1.1-r3') {
exclude group: 'com.android.support', module: 'support-v4'
}
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
androidTestCompile('org.robolectric:robolectric:2.3') {
exclude module: 'classworlds'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-plugin-registry'
exclude module: 'maven-profile'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'nekohtml'
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-http-shared'
exclude module: 'wagon-provider-api'
}
androidTestCompile 'com.squareup:fest-android:1.0.8'
}
我的目录结构如下,其中com.example.app.espresso
需要以connectedAndroidTest
和com.example.app.data
作为test
运行:
src |- debug |- main |- release |- test |- java |- com |- example |- app |- espresso |- HomeActivityTest.java |- data |- DataTest.java |- resources |- data_input.json
因此,当我运行gradle clean test
时,我发现错误无法识别HomeActivityTest.java
中的Espresso导入。
当我运行gradle clean connectedAndroidTest
时,我发现错误无法识别DataTest.java
(FailedToCreateTests.testSuiteConstructionFailed
)中的JUnit4注释。
如果我采取任何一部分(依赖关系和来源),另一部分可以独立工作,但不能包含在一起。
注意:我尝试在本地导入Espresso jar(没有Double Espresso),这与deckard-gradle相同,直到我使用support-v4
库中的任何内容在Espresso测试中(com.jakewharton.espresso:espresso-support-v4
似乎解决了这个问题,除了本地jar之外没有其他选择),然后它会爆炸成FailedToCreateTests.testSuiteConstructionFailed
。
有没有人有这个结构工作?有没有办法从每个目标中排除源路径?
任何解决方案(全部或部分)将不胜感激。
答案 0 :(得分:5)
这是因为Double Espresso工件被分发为.aar文件,而Robolectric为运行测试而生成的编译任务不依赖于解包作为androidTestCompile依赖关系配置一部分的.aar文件的任务。
由于您通常不会将espresso测试作为运行单元测试的任务的一部分运行,因此您可以安全地从Robolectric插件生成的编译任务中排除espresso测试。我这样做是通过将Robolectric插件生成的编译任务添加到我的build.gradle中来触及源属性。示例代码如下。确保修改robolectric生成的编译任务的名称(在我的示例中为'compileTestDebugJava'),并根据需要修改espresso测试的'exclude'。
tasks.whenTaskAdded { theTask ->
if ("compileTestDebugJava".toString().equals(theTask.name.toString())) {
def cleanupTask = "touchUpRobolectricSourceSet"
project.task(cleanupTask) << {
FileTree tree = fileTree(dir: 'src/test/java')
tree.exclude '**/espresso/**/*.java'
theTask.source = tree
}
theTask.dependsOn(cleanupTask)
}
}
答案 1 :(得分:1)
最终,我放弃了使用Double Espresso和继续使用deckard-gradle的方法的想法 - 手动导入Espresso jar(espresso
,testrunner
和testrunner-runtime
)。< / p>
似乎Double Espresso不仅仅是将罐子包装成aar并托管它们,或者它们是aar的原因导致问题。有兴趣知道为什么。
为了避免保留本地依赖关系,我将Espresso jar上传到Maven仓库,以菊花链式连接它们(espresso
取决于testrunner-runtime
,testrunner-runner
取决于testrunner
)并包含POM中的所有第三方依赖项(Guava,Hamcrest,Dagger等)。如果您没有托管的Maven仓库,可以使用GitHub作为您的仓库:https://stackoverflow.com/a/14013645/818393。
不可否认,这不是最佳解决方案,但可行。
答案 2 :(得分:0)
我有同样的问题,似乎是Dagger 1.2的错(包含在Jake&#39的双浓咖啡中)。如果包含plain 1.1库,它应该可以工作。
(完整主题:https://groups.google.com/forum/#!topic/robolectric/xeLrnCAsq5Q)