在 Android 项目中使用 Espresso 2.0 + Roboelectric

Ali*_*Ali 5 android gradle robolectric android-espresso

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

Roboelectric引入了decard-gradle模板项目来解决Roboelectric和Espresso一起使用的问题。但解决方案适用于 Espresso 1.1,现已弃用。

这是我的 build.gradle 文件的一部分,以便在我拥有 Roboelectric 以及遵循Espresso 2.0 指令时使用 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'
    }
}
Run Code Online (Sandbox Code Playgroud)

但我无法单独运行 Roboelectric 和 Espresso 测试。我很感激任何建议。

附加物:

to run roboelectric test : gradlew test
to run espresso: gradlew connectedAndroidTest
Run Code Online (Sandbox Code Playgroud)

fhs*_*lva 3

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

它有很多特点

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