配置 build.gradle 來用 Spock 對 Android 組件進行測試

在“使用 Android Studio 開發 Web 程序 - 測試”中說明了為什么要選擇 Spock Framework 來做為測試時的框架。在操作時,build.gradle 要做一定程度上的配置,才能夠使以 Spock 寫出來的源代碼得以運作,以下將會說明相關配置上的細節。

首先,Spock 的測試源代碼需要使用 Groovy 語言來編寫,所以 build.gradle 中要先引用 Groovy 的 Gradle Plugin。第一步要在 Root 的 build.gradle 中增加以下內容:

buildscript {
    ...
    dependencies {
        ...
        classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.1.0'
    }
}

第二步是在要使用 Groovy 的項目 build.gradle 中增加 apply plugin: 'groovyx.android’ 的內容,讓 Gradle 可以正確地識別 Groovy 所寫的源代碼。

第三步要配置在封裝 Apk 的過程中被排除的文件:

packagingOptions {
    exclude 'META-INF/services/org.codehaus.groovy.transform.ASTTransformation'
    exclude 'META-INF/services/org.codehaus.groovy.runtime.ExtensionModule'
}

完成以上的工作就可以配置 Dependencies 以便在撰寫程序時可以引用到 Spock Framework 里的 Class。但是光只有 Spock 也只能對一般的 Class 進行測試,如果要測試 Android 控件,牽涉到 Context 的問題,還是要使用官方的 Espresso 或是 Robolectric

Espresso 的問題不大,只要把 Java 的寫法改成 Groovy 套到 Spock 的 Class 結構里就行了。Robolectric 則是要改為引用另一個包:Robospock,依照 Robospock 的官方文件的說明,就可以順利的使用 Spock 來測試 Android 的控件。RoboSpock 就已經有引用 Robolectric,所以自己的 build.gradle 并不需要再配置一次。只是必須受限于 RoboSpock 生成時所配置的 Robolectric 版本,不一定能使用最新版的 Robolectric。

最后一個要注意的事項是,Espresso 在運行前是要經過實際封裝 Apk 的過程,所以 Groovy 在引用時要使用特別為 Android 開發的版本 - org.codehaus.groovy:groovy:2.4.7:grooid,否則很容易會出現超出 64K 的問題。

以下是完整的 build.gradle 的演示內容:

apply plugin: 'com.android.application'
apply plugin: 'groovyx.android'

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.0"

    defaultConfig {
        applicationId "com.example.sampleapp”
        minSdkVersion 9
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    testOptions {
        unitTests.returnDefaultValues = true
    }
    packagingOptions {
        exclude 'META-INF/services/org.codehaus.groovy.transform.ASTTransformation'
        exclude 'META-INF/services/org.codehaus.groovy.runtime.ExtensionModule'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'org.codehaus.groovy:groovy-all:2.4.7'
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    testCompile 'org.robospock:robospock:1.0.1'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestCompile 'org.codehaus.groovy:groovy:2.4.7:grooid'
    androidTestCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
        exclude group: 'org.codehaus.groovy'
        exclude group: 'junit'
    }
}

撰寫 Spock 測試前要先手動在 androidTest 或是 test 路徑下增加 groovy 的文件夾,其下再依據所屬的 Package 產生路徑結構。

最后,由于相容性的關系,在編譯的過程中會持續出現以下的訊息,但實際操作時并沒有阻擋或影響到測試程序的運行,所以可以忽略、不用理會。

:module:transformClassesWithDexForDebugAndroidTest
AGPBI: {"kind":"error","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]}
AGPBI: {"kind":"error","text":"(groovyjarjarantlr.TokenStreamRewriteEngine$1) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]}
AGPBI: {"kind":"error","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]}
AGPBI: {"kind":"error","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]}
AGPBI: {"kind":"error","text":"indicate that it is *not* an inner class.","sources":[{}]}
AGPBI: {"kind":"error","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]}
AGPBI: {"kind":"error","text":"(groovyjarjarantlr.build.ANTLR$1) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]}
AGPBI: {"kind":"error","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]}
AGPBI: {"kind":"error","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]}
AGPBI: {"kind":"error","text":"indicate that it is *not* an inner class.","sources":[{}]}
AGPBI: {"kind":"error","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]}
AGPBI: {"kind":"error","text":"(groovyjarjarantlr.debug.misc.ASTFrame$1) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]}
AGPBI: {"kind":"error","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]}
AGPBI: {"kind":"error","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]}
AGPBI: {"kind":"error","text":"indicate that it is *not* an inner class.","sources":[{}]}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評論 25 708
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,981評論 19 139
  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,568評論 2 45
  • 因為地域和家庭傳統的不同而不同
    teresa347閱讀 288評論 0 0
  • 轉自《ios應用逆向工程 分析與實戰》 -------------------------------------...
    曖曖閱讀 567評論 0 1