Espresso(-環境搭建)

(注意:本文針對Android Studio)

Espresso簡介

一、Espresso是google發佈的一款針對android UI進行測試的框架。

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

二、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.htmlandroid

三、Espresso官網地址:

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

四、Espresso的學習通過總結

    關於環境搭建的過程當中,我遇到了好幾個問題也耗費了很長的時間。不過這一次總結的經驗教訓就是下一次若是有對應的sample project的時候,在我遇到困難的時候。必定要先download。對照sample 進行modify.這樣會加快我解決問題的速度。github

Espresso環境搭建的過程

一、組建測試環境

    關閉測試機三個選項。具體操做步驟以下:web

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

  • Window animation scaleapp

  • Transition animation scale框架

  • Animator duration scalemaven

二、配置./app/build.gradle文件。下載Espresso相對應的依賴jar包

    第一步:肯定SDK中已經下載了最新版的 Android Support Repository學習

                以下圖所示:

        

    第二步:直接修改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'
}
三、也能夠直接在如下的路徑中直接下載對應的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中要求添加的部分(必選項)
    }
相關文章
相關標籤/搜索