微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android – Double Espresso不能与Robolectric合作

我试图通过仪器测试和单元测试通过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作为测试运行:

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.

有没有人有这个结构工作?有没有办法排除每个目标的源路径?

任何解决方案(全部或部分)将不胜感激.

解决方法

这是因为Double Espresso工件以.aar文件分发,Robolectric为运行测试而生成的编译任务不依赖于解包为androidTestCompile依赖关系配置一部分的.aar文件的任务.

由于您通常不会运行您的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)
    }
}

原文地址:https://www.jb51.cc/android/311679.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐