首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Studio + Spek集成

Android Studio + Spek集成
EN

Stack Overflow用户
提问于 2018-01-08 18:24:45
回答 2查看 1.9K关注 0票数 5

我正在尝试将Spek测试框架添加到我的Android项目中。按照这里的说明,我最后将以下内容添加到模块build.gradle

代码语言:javascript
复制
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

知道我错过了什么吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-24 16:54:24

(供日后参考)

要在Android中使用Kotlin和Spek + JUnit5,您需要以下内容:

在项目的build.gradle中,您需要:

代码语言:javascript
复制
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中,您需要:

代码语言:javascript
复制
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测试

代码语言:javascript
复制
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)
        }
    }
})

看见

票数 7
EN

Stack Overflow用户

发布于 2018-01-09 07:49:44

显然不是很好..。最后我使用了kotlintest,它更容易集成

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48155984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档