Android Studio + Spek 集成

2024-02-21

我正在尝试将 Spek 测试框架添加到我的 Android Studio 项目中。按照说明进行操作Here http://spekframework.org/docs/latest/,我最终将以下内容添加到我的模块中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

知道我错过了什么吗?


(供以后参考)
要在 Android Studio 中使用 Kotlin 和 Spek + JUnit5,您需要以下内容:

In 项目的 构建.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"
    }
}

In module's 构建.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)
        }
    }
})

See
- 官方文档http://spekframework.org/ http://spekframework.org/
- 用于从 IDE 运行规范的官方插件https://github.com/raniejade/spek-idea-plugin https://github.com/raniejade/spek-idea-plugin
- 使用 Spek 框架进行测试 - BDD 风格与 JUnit 风格https://www.youtube.com/watch?v=asDZ_7ZUiX4 https://www.youtube.com/watch?v=asDZ_7ZUiX4
- Spek和JUnit5的简单Android配置https://gist.github.com/Mugurell/088daf42a4d60240ba6993681e0537a5 https://gist.github.com/Mugurell/088daf42a4d60240ba6993681e0537a5

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Studio + Spek 集成 的相关文章