在“使用 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":[{}]}