如何运行Travis-CI和Espresso测试

9
我目前已经设置好了Travis-CI,每次构建我的Android设备时都会运行gradle ConnectedCheck任务并执行所有单元测试。我已经成功地设置了这一点。现在我正在尝试使用Espresso构建一些功能测试,但是我目前遇到了很多困难,因为我无法将Travis设置成这样,以便我的Espresso测试可以与Travis的模拟器进行交互。我该如何设置Travis,使其模拟器的工作方式与我在本地工作站上使用的完全相同?
以下是我在.travis.yml文件中用来构建模拟器的部分内容。
language: android
jdk: oraclejdk7
env:
  matrix:
    - ANDROID_TARGET=android-19  ANDROID_ABI=armeabi-v7a

android:
  components:
    - build-tools-20.0.0
    - build-tools-19.1.0

before_script:
  # Create and start emulator
  - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI
  - emulator -avd test -no-skin -no-audio -no-window &
  - android-wait-for-emulator
  - adb shell input keyevent 82 &
2个回答

7

更新于2015年9月7日

在Travis CI上成功运行安卓支持库中的Espresso是非常令人沮丧的。以下是我成功运行的确切配置。特定的sdk和库版本号很重要,所以不要更改它们,否则你会遇到问题。支持注释的解决方案也是必要的,因此不要删除它。由于Travis CI的android支持仍处于测试版,所以这个答案可能会过时。请查看Travis CI网站这里获取更新。

.travis.yml

language: android
jdk: openjdk7
android:
  components:
    - build-tools-22.0.1
    - android-20
    - extra
    - addon
    - sys-img-armeabi-v7a-android-19
before_script:
  - echo no | android create avd --force -n test -t android-19 --abi armeabi-v7a
  - emulator -avd test -no-skin -no-audio -no-window &
  - android-wait-for-emulator
  - adb shell input keyevent 82 &
script:
    - ./gradlew connectedAndroidTest

build.gradle

apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.application'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
    }
}

android {
    compileSdkVersion 20
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 20
        testApplicationId "com.example.app.test"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'com.android.support:support-v4:20.+'
    compile 'joda-time:joda-time:2.3'
    compile 'com.squareup.retrofit:retrofit:1.4.1'
    compile 'com.squareup.retrofit:retrofit-converters:1.9.0'
    compile 'com.squareup.retrofit:retrofit-mock:1.4.0'
    compile 'com.fasterxml.jackson.core:jackson-core:2.3.1'
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.3.0'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.3.1'
    compile 'com.google.guava:guava:16.0'
    androidTestCompile 'com.android.support:support-annotations:20.+'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.squareup:fest-android:1.0.7'
}

configurations.all {
    resolutionStrategy {
        // fail eagerly on version conflict (includes transitive dependencies)
        // e.g. multiple different versions of the same dependency (group and name are equal)
        failOnVersionConflict()

        // force certain versions of dependencies (including transitive)
        //  *append new forced modules:
        force 'com.android.support:support-annotations:20.+', 'com.squareup.retrofit:retrofit:1.4.1'
        //  *replace existing forced modules with new ones:
        forcedModules = ['com.android.support:support-annotations:20.+', 'com.squareup.retrofit:retrofit:1.4.1']

        // cache dynamic versions for 10 minutes
        cacheDynamicVersionsFor 10*60, 'seconds'
        // don't cache changing modules at all
        cacheChangingModulesFor 0, 'seconds'
    }
}

如果您收到类似于以下错误

PerformException: Error performing 'single click' on view

将此代码添加到测试中:

closeSoftKeyboard();
Thread.sleep(1000);

示例

public void testThatSuccessDialogIsShownWithValidCardInput() throws Exception {
    onView(withId(R.id.card_number))
            .perform(typeText("4242424242424242"));
    closeSoftKeyboard();
    Thread.sleep(1000);
    onView(withId(R.id.card_exp_month))
            .perform(typeText("01"));
    onView(withId(R.id.card_exp_year))
            .perform(typeText("20"));
    onView(withId(R.id.card_cvc_code))
            .perform(typeText("313"));
    closeSoftKeyboard();
    Thread.sleep(1000);
    onView(withText("Submit"))
            .perform(click());
    onView(withText("Success!"))
            .check(matches(isDisplayed()));
    onView(withText("OK"))
            .perform(click());
    onView(withText("OK"))
            .check(doesNotExist());
}

工作项目

https://travis-ci.org/Kurry/Venmo-Android-Coding-Challenge是本项目的构建状态,https://github.com/Kurry/Venmo-Android-Coding-Challenge是项目代码的存放地址。请点击链接了解详情。


2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接