【问题标题】:Can I use Spock test in Koltin Multiplatform project? If so how to set in gradle.build.kts?我可以在 Kotlin Multiplatform 项目中使用 Spock 测试吗?如果是这样,如何在 gradle.build.kts 中设置?
【发布时间】:2021-05-23 20:52:50
【问题描述】:

我尝试在我的 Kotlin 多平台项目中使用 Spock 测试框架,但失败了,gradle 脚本:

plugins {
    val kotlinVersion = "1.4.30"
    kotlin("multiplatform") version kotlinVersion
    application
    kotlin("plugin.serialization") version kotlinVersion
    groovy // for spock
}
repositories {
    // ...
    maven("https://jitpack.io") // for spock
}
kotlin {
    jvm {withJava()}
    js {/*...*/}
    sourceSets {
        val commonMain by getting {
            dependencies {/*...*/}
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val jvmMain by getting {
            dependencies {/*...*/}
        }
        val jvmTest by getting {
            dependencies {
                dependsOn(commonTest)
                implementation(kotlin("test"))
                implementation(kotlin("test-junit"))
                // implementation(kotlin("test-junit5"))
                // for spock
                implementation("org.spockframework.spock:spock-core:spock-1.3")
                implementation("org.codehaus.groovy:groovy-all:3.0.7")
            }
        }
        val jsMain by getting {
            dependencies {/*...*/}
        }
    }
}

我用groovy-2.5Junit5尝试了spock-1.3spock-2.0-M4,都因依赖问题而失败,即使测试可以运行,它告诉:没有收到测试事件 ,更重要的是:

不能仅为测试创建 groovy 源文件夹。(该目录类似于普通文件夹,而不是源或测试文件夹)

如何配置依赖或gradle.build.kts文件?非常感谢任何帮助!

【问题讨论】:

    标签: kotlin spock kotlin-multiplatform


    【解决方案1】:

    我现在可以使 Spock 与现有的 Ktor 项目一起工作,并将以下代码添加到我的 gradle.build.kts 中:

    plugins {
        groovy
    }
    
    repositories {
        maven("https://jitpack.io")
    }
    
    dependencies {
        testImplementation("org.codehaus.groovy:groovy-all:2.4.15")
        testImplementation("org.spockframework.spock:spock-core:spock-1.3")
    }
    
    sourceSets {
        main {
            withConvention(GroovySourceSet::class) {
                groovy {
                    setSrcDirs(listOf("src"))
                }
            }
        }
        test {
            withConvention(GroovySourceSet::class) {
                groovy {
                    setSrcDirs(listOf("test"))
                }
            }
        }
    }
    
    tasks.test {
        useJUnitPlatform()
        testLogging {
            events("passed", "skipped", "failed")
        }
    }
    

    但是,我无法通过 Spock 测试找出 Koltin Multiplatform 项目,我会在制作时发布脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-23
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多