android - 在 Android 项目中使用 Espresso 2.0 + Roboelectric

标签 android gradle robolectric android-espresso

我已经进行了一堆 Roboelectric 测试。我想添加 Espresso 2.0这是最近推出的。

Roboelectric 介绍 deckard-gradle template project解决 Roboelectric 和 Espresso 一起使用的问题。但解决方案适用于现在已弃用的 Espresso 1.1。

这是我的 build.gradle 文件的一部分,以便在我拥有 Roboelectric 以及以下 Espresso 2.0 instruction 后使用 Espresso 2.0 :

buildscript {
    repositories {
        mavenCentral()
        maven { url 'http://download.crashlytics.com/maven' }
    }

    dependencies {
        classpath 'org.robolectric:robolectric-gradle-plugin:0.13.2'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'robolectric'

android {
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
    }
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        versionCode 1
        versionName '1.0'
        minSdkVersion 9
        targetSdkVersion 21
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

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

dependencies {
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'

    androidTestCompile('junit:junit:4.11') {
        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'
        exclude group: 'com.android.support', module: 'appcompat-v7'
        exclude group: 'com.android.support', module: 'support-v4'
    }
}

但我无法单独运行 Roboelectric 和 Espresso 测试。感谢您提出任何建议。

附录:

to run roboelectric test : gradlew test
to run espresso: gradlew connectedAndroidTest

最佳答案

试试这个 android 入门项目: https://github.com/freezy/android-seed

它有很多features :

  • 在 Android Studio 中很好地加载
  • Robolectric 测试在 IDE 中运行并可调试(用于单元测试)
  • Espresso 测试在 IDE 中工作并可调试(用于功能测试)
  • 最新的 Lollipop 兼容库可供使用
  • 启用测试覆盖率

关于android - 在 Android 项目中使用 Espresso 2.0 + Roboelectric,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28066216/

相关文章:

android - 从 androi.jar 获取 native android 图标时出错

java - 应用程序被终止时服务正在重新启动

android - Gradle - 无法使用 gradle 依赖项在 Android Studio 中导入 viewpagerindicator

Android studio 2.3 刷新gradle project taking forever

使用 Robolectric ClassCastException : android. app.Application 进行的 Android 测试无法转换为 MyApplication

android - 从 Firebase 数据创建一个 ArrayList

android - 从操作栏中删除 Logo 和文本并用图标替换它们

android - 如何在我的自定义Gradle插件中获取compileSdkVersion

Robolectric 测试找不到字体文件

android - Robolectric 3.3 与 Theme.AppCompat