Espresso(-环境搭建)

(注意:本文针对Android Studio)

Espresso简介

1、Espresso是google发布的一款针对android UI进行测试的框架。

Espresso is targeted at developers, who believe that automated testing is an integral part of the development lifecycle.

2、Espresso支持的android版本如下:
Codename API
Froyo 8
Gingerbread 10
Ice Cream Sandwich 15
Jelly Bean 16, 17 ,18
KitKat 19
Lollipop 21

参考地址:https://google.github.io/android-testing-support-library/docs/espresso/index.html

3、Espresso官网地址:

https://google.github.io/android-testing-support-library/docs/espresso/index.html

4、Espresso的学习经过总结

    关于环境搭建的过程中,我遇到了好几个问题也耗费了很长的时间。不过这一次总结的经验教训就是下一次如果有对应的sample project的时候,在我遇到困难的时候。一定要先download。对照sample 进行modify.这样会加快我解决问题的速度。

Espresso环境搭建的过程

1、组建测试环境

    关闭测试机三个选项。具体操作步骤如下:

On your device, under Settings->Developer options disable the following 3 settings:

  • Window animation scale

  • Transition animation scale

  • Animator duration scale

2、配置./app/build.gradle文件。下载Espresso相对应的依赖jar包

    第一步:确定SDK中已经下载了最新版的 Android Support Repository

                如下图所示:

        Espresso(-环境搭建)_第1张图片

    第二步:直接修改app下面的build.gradle文件,下载需要的jar包

        下面的build.gradle文件,是我的项目中的,其中标注-----Espresso中要求添加的部分

        就是Espresso需要添加的jar包。其中分为了必选项和可选项。根据需要进行选择添加

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    defaultConfig {
        applicationId "com.collection.self.com.espressotest"
        minSdkVersion 10
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        //-----Espresso中要求添加的部分(必选项)
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
//    testCompile 'junit:junit:4.12'----这个一定要删除,否则AndroidJUnit4无法下载下来
    androidTestCompile 'com.android.support:support-annotations:23.1.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
// Android JUnit Runner-----Espresso中要求添加的部分(必选项)
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    // JUnit4 Rules-----Espresso中要求添加的部分(必选项)
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    // Espresso core-----Espresso中要求添加的部分(必选项)
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    // Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource
    //-----Espresso中要求添加的部分(可选项)
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
    // Espresso-web for WebView support-----Espresso中要求添加的部分(可选项)
    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.1'
    // Espresso-idling-resource for synchronization with background jobs-----Espresso中要求添加的部分(可选项)
    androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:2.2.1'
}
3、也可以直接在以下的路径中直接下载对应的jar包。添加到项目中去

https://github.com/googlesamples/android-testing/tree/master/ui/espresso/BasicSampleBundled/libs

参考地址:https://google.github.io/android-testing-support-library/downloads/index.html

Espresso环境搭建过程中遇到的问题已经解决方案

【问题一】

报错内容如下:

 Error:Conflict with dependency 'com.android.support:support-annotations'.
 Resolved versions for app (23.1.1) and test app (23.0.1) differ.
 See http://g.co/androidstudio/app-test-app-conflict for details.

解决方案如下:

 在app/build.gradle中添加一个设置就可以了
 androidTestCompile 'com.android.support:support-annotations:23.1.1'

 这个在上面给出的build.gradle文件中已经贴出来了。

下面是对这个问题的英文解释:

Resolving conflicts between main and test APK

When instrumentation tests are run, both the main APK and test APK share the same classpath.
Gradle build will fail if the main APK and the test APK use the same library (e.g. Guava) but in different versions.
If gradle didn't catch that, your app could behave differently during tests and during normal run (including crashing in one of the cases).

To make the build succeed, just make sure both APKs use the same version.
If the error is about an indirect dependency (a library you didn't mention in your build.gradle),
just add a dependency for the newer version to the configuration ("compile" or "androidTestCompile") that needs it.
 You can also use Gradle's resolution strategy mechanism.
 You can inspect the dependency tree by running ./gradlew :app:dependencies and ./gradlew :app:androidDependencies.

(英文不是很好,就不翻译了)

【问题二】

报错内容如下:

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebugAndroidTest'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException:
Duplicate files copied in APK META-INF/maven/com.google.guava/guava/pom.properties
 File1: F:\workspace\UnitTestProject\app\build\intermediates\exploded-aar\com.android.support.test.espresso\espresso-web\2.2.1\jars\classes.jar
 File2: F:\workspace\UnitTestProject\app\build\intermediates\exploded-aar\com.android.support.test.espresso\espresso-core\2.2.1\jars\classes.jar

解决方案如下:

在app/build.gradle文件中添加如下配置:

packagingOptions {
      exclude 'META-INF/maven/com.google.guava/guava/pom.properties'
      exclude 'META-INF/maven/com.google.guava/guava/pom.xml'
    }

参考地址:

    http://stackoverflow.com/questions/33800924/espresso-web-import-causes-duplicatefileexception
    https://github.com/googlesamples/android-testing/blob/master/ui/espresso/WebBasicSample/app/build.gradle

【问题三】

Espresso无法import AndroidJUnit4

解决方案如下:

app/build.gradle文件中不可出现如下配置项

  testCompile 'junit:junit:4.12'

必须添加如下红色部分标出的配置项

dedefaultConfig {
    applicationId "com.collection.self.com.espressotest"
    minSdkVersion 9
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    //-----Espresso中要求添加的部分(必选项)
    }

你可能感兴趣的:(Espresso(-环境搭建))