(供将来参考)
要在 Android Studio 中使用 Kotlin 和 Spek + JUnit5,您需要以下内容:
在项目的 build.gradle 中,您需要:
buildscript {
ext.kotlin_version = '1.2.10'
ext.JUnit5_version = '1.0.30'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "de.mannodermaus.gradle.plugins:android-junit5:$JUnit5_version"
}
}
在模块的 build.gradle 中,您需要:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "de.mannodermaus.android-junit5"
android {
...
sourceSets {
test.java.srcDirs += 'src/test/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
}
project.ext {
spekVersion = "1.1.5"
}
dependencies {
...
//
// TESTS
testImplementation("org.jetbrains.spek:spek-api:$spekVersion") {
exclude group: "org.jetbrains.kotlin"
}
testImplementation("org.jetbrains.spek:spek-junit-platform-engine:$spekVersion") {
exclude group: "org.junit.platform"
exclude group: "org.jetbrains.kotlin"
}
testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testImplementation junit5.unitTests()
// see https://github.com/mannodermaus/android-junit5#android-studio-workarounds
testCompileOnly junit5.unitTestsRuntime()
}
简单的 Spek 测试
class ExampleSpekTest : Spek({
val x = 2
val y = 3
given("x = $x and y = $y") {
val sum = x + y
it("should be that x + y = 5") {
Assert.assertEquals(5, sum)
}
it("should be that x - y = -1") {
val subtract = x - y
Assert.assertEquals(-1, subtract)
}
}
})
请参阅
- 官方文档http://spekframework.org/
- 用于从 IDE 运行规范的官方插件https://github.com/raniejade/spek-idea-plugin
- 使用 Spek 框架进行测试 - BDD 样式与 JUnit 样式https:// /www.youtube.com/watch?v=asDZ_7ZUiX4
- Spek 和 JUnit5 的简单 Android 配置https://gist.github.com/Mugurell/088daf42a4d60240ba6993681e0537a5