unit-testing - Android Studio + Spek 集成

标签 unit-testing junit kotlin android-studio-3.0 spek

我正在尝试将 Spek 测试框架添加到我的 Android Studio 项目中。按照说明Here ,我最终将以下内容添加到我的模块 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:没有至少一个 TestEngine 就无法创建启动器;考虑将引擎实现 JAR 添加到类路径

知道我错过了什么吗?

最佳答案

(供日后引用)
要在 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

关于unit-testing - Android Studio + Spek 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48155984/

相关文章:

string - 删除 kotlin 中的重音和变音符号

java - 创建套件时如何让 Eclipse 识别 JUnit 测试?

android - 将第二个 Room 数据库添加到我的应用程序会导致异常 : "Caused by: java.lang.IllegalStateException: Room cannot verify the data integrity"

asp.net-mvc - 如何在 Web API ASP.Net core 中对异常过滤器进行单元测试。不想模拟 onException 方法

angular - 模拟 http promise 服务时出现 `then is not a function` 错误

java - 测试中奇怪的上下文行为

java - 使用 Junit 测试抛出异常的方法

android - 如何在 Kotlin 中设置支持缩放

unit-testing - 如何对phpunit中不返回值的函数进行单元测试?

javascript - 如何对页面导航进行单元测试?