在Android项目中使用Espresso 2.0 + Robolectric

时间:2015-01-21 11:34:09

标签: 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 Espresso 2.0 instruction

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

2 个答案:

答案 0 :(得分:3)

尝试使用此Android启动项目: https://github.com/freezy/android-seed

它有很多features

  • 在Android Studio中很好地加载
  • Robolectric在IDE中测试工作和可调试(用于单元测试)
  • Espresso测试在IDE中工作和调试(用于功能测试)
  • 最新的Lollipop compat libs准备好使用
  • 启用测试覆盖率

答案 1 :(得分:1)

我排除了测试来完成你想要的任务。

sourceSets {
    androidTest {
        java {
            exclude 'com/company/project/**/*Test.java'
        }
    }
}

gradle.taskGraph.whenReady {taskGraph ->
    def testRunTask = project.tasks.getByName 'testDebug'
    testRunTask.include(['**/*Test.class'])
    testRunTask.exclude(['**/espresso/**/*.class','**/integration/**/*.class'])
}