注意: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作为测试运行:
src |- debug |- main |- release |- test |- java |- com |- example |- app |- espresso |- HomeActivityTest.java |- data |- DataTest.java |- resources |- data_input.json
所以当我运行gradle clean测试,我得到错误不认识Espresso进口在HomeActivityTest.java.
当我运行gradle clean connectedAndroidTest时,我收到错误不识别在DataTest.java中的JUnit4注释(FailedToCreateTests.testSuiteConstructionFailed).
如果我把任何一部分(依赖和来源),另一个单独工作,但不包括在一起的一切.
注意:我尝试在本地进口Espresso jar(不含双重浓缩咖啡),同样的方式,它们可以在Espresso测试(com.jakewharton.espresso:espresso-support-v4)中使用任何来自support-v4库的工作似乎解决了,这是没有替代本地jar的),那么它会爆炸成FailedToCreateTests.testSuiteConstructionFailed.
有没有人有这个结构工作?有没有办法排除每个目标的源路径?
任何解决方案(全部或部分)将不胜感激.
解决方法
由于您通常不会运行您的espresso测试作为运行单元测试的任务的一部分,您可以安全地从Robolectric插件生成的编译任务中排除espresso测试.我通过向Robolectric插件生成的编译任务添加一个依赖关系到我的build.gradle,它触发了source属性.下面的示例代码.确保在需要时触摸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) } }