使用Kotlin Gradle DSL将集成测试添加到Kotlin项目 [英] Adding integration tests to Kotlin project using the Kotlin Gradle DSL

查看:93
本文介绍了使用Kotlin Gradle DSL将集成测试添加到Kotlin项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向将包含集成测试的Kotlin项目添加一个额外的源集".我已经看到了几篇有关为香草Java项目或Kotlin进行此操作的文章,但使用的是Groovy而不是Kotlin Gradle DSL.

I would like to add an additional "source set" to a Kotlin project that will contain integration tests. I have seen a few posts that talk about doing it for either a vanilla Java project or for Kotlin but using Groovy rather than the Kotlin Gradle DSL.

总而言之,使用Kotlin Gradle DSL:

  • 如何添加额外的源集",其中可以包含Kotlin代码,Java代码和用来将集成测试与常规单元测试分开的资源?
  • 如何添加其他任务和配置以独立于单元测试运行集成测试?
  • how to add an additional "source set" that can contain Kotlin code, Java code & resources for the purpose of separating integration tests from regular unit tests?
  • how to add an additional task and configuration to run the integration tests separately from unit tests?

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

I would expect the directory structure to look something like:

src
   main
      java
      kotlin
      resources
   test
      java
      kotlin
      resources
   integration
      java
      kotlin
      resources

相关:

https://www. petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/

如何向其中添加新的源集摇篮?

谢谢

推荐答案

首先,创建源集和配置:

First, create source set and configuration:

sourceSets {
    create("intTest") {
        compileClasspath += sourceSets.main.get().output
        runtimeClasspath += sourceSets.main.get().output
    }
}

val intTestImplementation: Configuration by configurations.getting {
    extendsFrom(configurations.implementation.get())
}

val intTestRuntimeOnly: Configuration by configurations.getting {
    extendsFrom(configurations.runtimeOnly.get())
}

然后,创建任务以运行它们:

And then, create the task to run them:

val integrationTest = task<Test>("integrationTest") {
    description = "Runs integration tests"
    group = "verification"

    testClassesDirs = sourceSets["intTest"].output.classesDirs
    classpath = sourceSets["intTest"].runtimeClasspath
    shouldRunAfter("test")
}

此外,您可以添加新的源集要使用的依赖项.例如:

Also, you can add dependencies to be used by the new source set. For instance:

intTestImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
intTestRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")

这篇关于使用Kotlin Gradle DSL将集成测试添加到Kotlin项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆