我想向使用Gradle's JVM Test Suites的Kotlin项目添加一个集成测试套件,但由于缺少依赖项,它无法编译测试.我需要如何将集成测试指向常规测试的依赖项?

项目 struct (由gradle init生成,加上我添加的src/intTest)如下所示:

.
├── app
│   ├── build.gradle.kts
│   └── src
│       ├── intTest
│       │   └── kotlin
│       │       └── SomeTest.kt
│       ├── main
│       │   ├── kotlin
│       │   │   └── mcve
│       │   │       └── App.kt
│       │   └── resources
│       └── test
│           ├── kotlin
│           │   ├── SomeTest.kt
│           │   └── mcve
│           │       └── AppTest.kt
│           └── resources
├── gradle
│   ├── libs.versions.toml
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
└── settings.gradle.kts

app/src/test/kotlin/SomeTest.ktapp/src/intTest/kotlin/SomeTest.kt是这个MCVE的同一个文件.

import kotlinx.coroutines.*
import kotlin.test.assertEquals
import kotlin.test.Test

class SomeTest {
    @Test
    fun someTest() {
        assertEquals(1, 1)
    }
}

我在app/build.gradle.kts中添加了新的测试套件:

plugins {
    alias(libs.plugins.jvm)
    application
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
}

testing {
    suites {
        val test by getting(JvmTestSuite::class) {
            useKotlinTest("1.9.20")
        }
        // Added the new test suite here:
        val intTest by registering(JvmTestSuite::class) {
            dependencies {
                implementation(project())
            }
        }
    }
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(21))
    }
}

application {
    mainClass.set("mcve.AppKt")
}

当我运行./gradlew test时,它可以工作,但当我运行./gradlew intTest时,它失败了,因为它既找不到生产依赖项(协 routine ),也找不到测试依赖项(Ktest):

> Task :app:compileIntTestKotlin FAILED
e: file:///Users/robert/mcve/app/src/intTest/kotlin/SomeTest.kt:1:8 Unresolved reference: kotlinx
e: file:///Users/robert/mcve/app/src/intTest/kotlin/SomeTest.kt:1:15 Unresolved reference: test
e: file:///Users/robert/mcve/app/src/intTest/kotlin/SomeTest.kt:2:15 Unresolved reference: test
e: file:///Users/robert/mcve/app/src/intTest/kotlin/SomeTest.kt:5:6 Unresolved reference: Test
e: file:///Users/robert/mcve/app/src/intTest/kotlin/SomeTest.kt:7:9 Unresolved reference: assertEquals

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileIntTestKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

我需要如何配置Gradle 8.5(修订版28aca86a7180baa17117e0e5ba01d8ea9fea598)才能在app/src/intTest中运行集成测试?

推荐答案

Kotlin测试库和主要源集依赖项没有传递到集成测试套件有不同的原因.

Adding the Kotlin test library

调用useKotlinTest()会添加依赖项,但仅限于被调用的套件.因此,您需要 for each 测试套件调用它.这似乎是测试套件功能1中的错误.

您可以使用以下命令为所有套件进行配置:

testing {
    suites {
        withType<JvmTestSuite>().configureEach {
            useKotlinTest("1.9.20")
        }
    }
}

Depending on the main project dependencies

调用implementation(project())不会添加对主源代码集的编译依赖项配置的依赖项2.

添加这些的一种方法是使新测试套件的implementation配置扩展主源代码集的implementation配置:

val intTest by registering(JvmTestSuite::class) {
    dependencies {
        implementation(project())
    }
    configurations {
        named(sources.implementationConfigurationName) {
            extendsFrom(getByName(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME))
        }
    }
}

这个话题也在this question节课中讨论过.


1documentation中介绍了测试框架的配置,其中脚注4说:

您不必在这些附加套件上显式地配置测试框架,除非您希望更改为另一个框架

这表明配置主测试套件的框架应该足够了,就像您所做的那样.我自己测试过它,我同意您的看法,在Kotlin测试框架的情况下,与文档相反,它不会向其他测试套件添加必要的依赖项.

2这在脚注3的第documentation条中进行了解释:

[project()]依赖项提供对项目输出以及在其apicompileOnlyApi配置上声明的任何依赖项的访问.

Kotlin相关问答推荐

在Kotlin Jetpack中重用下拉菜单

Kotlin中函数引用的否定

如何创建一个空的kotlin工作?

如何在Spring Boot中注册新的集合工厂

使用 kotlin 流删除 map 中具有某些相似性的值

Jetpack Compose - 单击 LazyColumn 的项目时应用程序崩溃

Mixin 在 Jackson 中添加 defaultImpl 不起作用

Kotlin 中私有集的完整语法 struct 是什么?

Jetpack BottomNavigation - java.lang.IllegalStateException:Already attached to lifecycleOwner

retrofit 响应代码 405 并带有消息method not allowed here

Kotlin 的 isNullOrBlank() 函数是否可以导入 xml 以用于数据绑定

在 Scaffold Jetpack Compose 内的特定屏幕上隐藏顶部和底部导航器

TextField maxLength - Android Jetpack Compose

Mockito 的 argThat 在 Kotlin 中返回 null

哪里可以找到aapt2日志(log)?

Android插件2.2.0-alpha1无法使用Kotlin编译

是否在Kotlin中重写enum toString()?

Kotlin/JS,Gradle 插件:无法加载@webpack-cli/serve命令

重复构建失败To use Coroutine features, you must add `ktx`......

如何在Kotlin中将字符串转换为InputStream?