猿问

使用 Kotlin Gradle DSL 向 Kotlin 项目添加集成测试

我想向包含集成测试的 Kotlin 项目添加一个额外的“源集”。我看过一些帖子,他们谈论为 vanilla Java 项目或 Kotlin 做这件事,但使用 Groovy 而不是 Kotlin Gradle DSL。

总之,使用 Kotlin Gradle DSL:

  • 如何添加一个额外的“源集”,它可以包含 Kotlin 代码、Java 代码和资源,以便将集成测试与常规单元测试分开?

  • 如何添加额外的任务和配置以将集成测试与单元测试分开运行?

我希望目录结构看起来像:

src

   main

      java

      kotlin

      resources

   test

      java

      kotlin

      resources

   integration

      java

      kotlin

      resources


翻阅古今
浏览 169回答 2
2回答

慕慕森

首先,创建源集和配置:sourceSets {&nbsp; &nbsp; create("intTest") {&nbsp; &nbsp; &nbsp; &nbsp; compileClasspath += sourceSets.main.get().output&nbsp; &nbsp; &nbsp; &nbsp; runtimeClasspath += sourceSets.main.get().output&nbsp; &nbsp; }}val intTestImplementation: Configuration by configurations.getting {&nbsp; &nbsp; extendsFrom(configurations.implementation.get())}val intTestRuntimeOnly: Configuration by configurations.getting {&nbsp; &nbsp; extendsFrom(configurations.runtimeOnly.get())}然后,创建任务来运行它们:val integrationTest = task<Test>("integrationTest") {&nbsp; &nbsp; description = "Runs integration tests"&nbsp; &nbsp; group = "verification"&nbsp; &nbsp; testClassesDirs = sourceSets["intTest"].output.classesDirs&nbsp; &nbsp; classpath = sourceSets["intTest"].runtimeClasspath&nbsp; &nbsp; shouldRunAfter("test")}此外,您可以添加要由新源集使用的依赖项。例如:intTestImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")intTestRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
随时随地看视频慕课网APP

相关分类

Java
我要回答