android - 运行 Spek 测试显示错误 "Empty test suite"

标签 android unit-testing android-studio testing kotlin

熟悉了 Kotlin 我想向它介绍另一个 Android-Java 项目,仅作为测试的第一步。我决定直接从 Spek 开始.

我在待测试模块的build.gradle中添加了以下依赖:

testCompile 'junit:junit:4.12'
testCompile "org.jetbrains.kotlin:kotlin-stdlib:1.0.2"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:1.0.2"
testCompile "org.jetbrains.spek:spek:1.0.25"

除此之外,我使用了 SimpleTest git 存储库的 spek-samples 类:

import org.jetbrains.spek.api.Spek
import kotlin.test.assertEquals


class SampleCalculator
{
    fun sum(x: Int, y: Int) = x + y
    fun subtract(x: Int, y: Int) = x - y
}

class SimpleTest : Spek({
    describe("a calculator") {
        val calculator = SampleCalculator()

        it("should return the result of adding the first number to the second number") {
            val sum = calculator.sum(2, 4)
            assertEquals(6, sum)
        }

        it("should return the result of subtracting the second number from the first number") {
            val subtract = calculator.subtract(4, 2)
            assertEquals(2, subtract)
        }
    }
})

代码编译后,在 Android Studio 中,类声明旁边会显示一个绿色播放按钮,用于运行该类的测试。然而,这样做会导致

Process finished with exit code 1
Class not found: "com.anfema.ionclient.serialization.SimpleTest"Empty test suite.

我创建了 JUnit3 和 JUnit4 测试,它们都运行没有任何问题。

我是否错过了 Spek 测试的任何额外配置步骤?

最佳答案

我的失败是我没有完全/正确配置 Kotlin。

我错过了应用 Kotlin 插件,这是通过以下几行完成的:

apply plugin: 'kotlin-android'

buildscript {
    ext.kotlin_version = '1.0.2'
    repositories {
        jcenter()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }

如果您想在普通 JUnit 测试中使用 Kotlin 代码,这也是一个要求。

关于android - 运行 Spek 测试显示错误 "Empty test suite",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37392478/

相关文章:

android - maven-metadata.xml 在构建时不断收到请求

android - Android 的接近传感器如何工作?

android - "private"aidl 对系统进程有多安全

java - 模拟被测试类中使用的不同类?

java - 如何在不使用嵌套循环的情况下解决java中的 "the birthday paradox"?

android - 按钮的属性编辑器弄乱了 onClick 值(在括号中插入类名)

Javascript require() 多次测试文件不会再次创建对象

java - 同一类中的模拟方法

ios - 如何在多操作系统项目中设置开发团队?

android - 任务 ':compileDebugJavaWithJavac' 的 Apache Cordova 执行失败