配置 build.gradle 來用 Spock 對(duì) Android 組件進(jìn)行測(cè)試

在“使用 Android Studio 開發(fā) Web 程序 - 測(cè)試”中說明了為什么要選擇 Spock Framework 來做為測(cè)試時(shí)的框架。在操作時(shí),build.gradle 要做一定程度上的配置,才能夠使以 Spock 寫出來的源代碼得以運(yùn)作,以下將會(huì)說明相關(guān)配置上的細(xì)節(jié)。

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

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

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

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

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

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

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

最后一個(gè)要注意的事項(xiàng)是,Espresso 在運(yùn)行前是要經(jīng)過實(shí)際封裝 Apk 的過程,所以 Groovy 在引用時(shí)要使用特別為 Android 開發(fā)的版本 - org.codehaus.groovy:groovy:2.4.7:grooid,否則很容易會(huì)出現(xiàn)超出 64K 的問題。

以下是完整的 build.gradle 的演示內(nèi)容:

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 測(cè)試前要先手動(dòng)在 androidTest 或是 test 路徑下增加 groovy 的文件夾,其下再依據(jù)所屬的 Package 產(chǎn)生路徑結(jié)構(gòu)。

最后,由于相容性的關(guān)系,在編譯的過程中會(huì)持續(xù)出現(xiàn)以下的訊息,但實(shí)際操作時(shí)并沒有阻擋或影響到測(cè)試程序的運(yùn)行,所以可以忽略、不用理會(huì)。

: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":[{}]}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,238評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,430評(píng)論 3 415
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,134評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,893評(píng)論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,653評(píng)論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,136評(píng)論 1 323
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,212評(píng)論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,372評(píng)論 0 288
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,888評(píng)論 1 334
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,738評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,939評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,482評(píng)論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,179評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,588評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,829評(píng)論 1 283
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,610評(píng)論 3 391
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,916評(píng)論 2 372

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,632評(píng)論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,781評(píng)論 18 139
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,465評(píng)論 2 45
  • 因?yàn)榈赜蚝图彝鹘y(tǒng)的不同而不同
    teresa347閱讀 252評(píng)論 0 0
  • 轉(zhuǎn)自《ios應(yīng)用逆向工程 分析與實(shí)戰(zhàn)》 -------------------------------------...
    曖曖閱讀 560評(píng)論 0 1