Android Studio + Spek集成 [英] Android Studio + Spek integration
问题描述
我正在尝试将Spek测试框架添加到我的Android Studio项目中.按照此处的说明,我最终在模块build.gradle
中添加了以下内容:
I'm trying to add Spek testing framework to my Android Studio project. Following the instructions Here, I ended up adding the following to my module build.gradle
:
testCompile 'org.jetbrains.spek:spek-api:1.1.5'
testCompile 'junit:junit:4.12'
testCompile "org.junit.platform:junit-platform-runner:1.0.0"
testRuntimeOnly 'org.jetbrains.spek:spek-junit-platform-engine:1.1.5'
然后我用@RunWith(JUnitPlatform::class)
但是,当我尝试运行测试时,我得到:
org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath
However, when I try to run the test, I get:
org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath
知道我想念什么吗?
推荐答案
(供将来参考)
要在Android Studio中使用Kotlin和Spek + JUnit5,您需要执行以下操作:
(For future references)
To use Kotlin and Spek + JUnit5 in Android Studio you need the following:
在项目的 build.gradle 中,您需要具备以下条件:
In project's build.gradle you need to have:
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 中,您需要具备以下条件:
In module's build.gradle you need to have:
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-插件
-使用Spek Framework进行测试-BDD样式与JUnit样式 https://www.youtube.com/watch? v = asDZ_7ZUiX4
-Spek和JUnit5的简单Android配置 https://gist.github.com/Mugurell/088daf42a4d60240ba6993681e0537a5
See
- official documentation http://spekframework.org/
- official plugin for running specs from the IDE https://github.com/raniejade/spek-idea-plugin
- Tests using Spek Framework - BDD Style vs JUnit Style https://www.youtube.com/watch?v=asDZ_7ZUiX4
- Simple Android configuration of Spek and JUnit5 https://gist.github.com/Mugurell/088daf42a4d60240ba6993681e0537a5
这篇关于Android Studio + Spek集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!