【问题标题】:Running Spek test shows error "Empty test suite"运行 Spek 测试显示错误“空测试套件”
【发布时间】:2016-05-23 13:34:36
【问题描述】:

对 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 repo 的 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 测试的任何其他配置步骤?

【问题讨论】:

    标签: android unit-testing android-studio testing kotlin


    【解决方案1】:

    我的失败是我没有完全/正确配置 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 代码,这也是一项要求。

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 2021-05-21
      • 2016-12-07
      相关资源
      最近更新 更多