Compose Multiplatform项目创建额外测试模块的配置问询
解决Compose Multiplatform额外测试模块配置问题
正确的Gradle配置方案
在项目的build.gradle.kts中添加以下配置,确保额外测试模块能正确识别依赖并运行:
1. 完整配置代码
plugins { kotlin("multiplatform") id("org.jetbrains.compose") } kotlin { jvm("desktop") { compilations.all { kotlinOptions.jvmTarget = "17" } testRuns["test"].executionTask.configure { useJUnitPlatform() } // 声明自定义测试编译单元 val desktopFullChainTest by compilations.creating { defaultSourceSet.apply { kotlin.srcDir("src/desktopFullChainTest/kotlin") resources.srcDir("src/desktopFullChainTest/resources") // 关联主模块源码,确保能访问业务代码 dependsOn(compilations["main"].defaultSourceSet) } // 配置测试依赖 dependencies { implementation(kotlin("test")) implementation(compose.uiTestJUnit4) // 可添加全链路测试专属依赖 } } // 创建对应的测试运行任务 val desktopFullChainTestTask by tasks.creating(Test::class) { description = "运行桌面端全链路验证测试(发布前执行)" group = "verification" // 确保类路径包含编译输出和所有依赖 classpath = desktopFullChainTest.output + desktopFullChainTest.runtimeClasspath useJUnitPlatform() include("**/*Test.kt") } } sourceSets { val desktopMain by getting // 注册自定义源码集,让IDE正确识别 val desktopFullChainTest by creating { dependsOn(desktopMain) dependencies { implementation(kotlin("test")) implementation(compose.uiTestJUnit4) } } } }
2. 对应目录结构
确保项目目录与配置匹配:
src/desktopFullChainTest/kotlin:存放全链路测试代码src/desktopFullChainTest/resources:测试相关资源文件
3. 运行测试命令
执行以下Gradle命令启动自定义测试:
./gradlew desktopFullChainTestTask
关键配置说明
- 关联主模块:通过
dependsOn(compilations["main"].defaultSourceSet)让测试模块能访问主模块的类,解决NoClassDefFoundError问题 - 完整类路径:测试任务的
classpath同时包含编译输出和运行时依赖,确保所有传递依赖被正确加载 - IDE识别支持:在
sourceSets中显式注册自定义源码集,让IDE能索引测试代码和依赖
内容的提问来源于stack exchange,提问作者jlengrand
相关产品推荐
相关产品推荐

