gradle-kotlin-dsl - 在不同模块的测试中使用测试包中的代码
问题描述
我在 kotlin 中有一个使用 gradle dsl 配置的“基础”项目。在基地内部,我有模块“应用程序”和模块“库”。模块应用程序使用模块库。我想从模块应用程序的测试中访问模块库的测试(准确地说是库的 testUtils 包)。
app.build gradle 文件有这样一行implementation(project(":library"))
,我无法在我的主代码中访问库的测试,这很好,但我也无法从应用程序的测试中访问它。
我读过这个问题(可能是这个问题的重复),其中相关的常规代码是:
task testJar(type: Jar, dependsOn: testClasses) {
baseName = "test-${project.archivesBaseName}"
from sourceSets.test.output
}
configurations {
tests
}
artifacts {
tests testJar
}
但我无法在 gradle dsl 中实现它。
我还检查了这个问题的代码在哪里:
tasks {
val sourcesJar by creating(Jar::class) {
dependsOn(JavaPlugin.CLASSES_TASK_NAME)
classifier = "sources"
from(sourceSets["main"].allSource)
}
val javadocJar by creating(Jar::class) {
dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
classifier = "javadoc"
from(tasks["javadoc"])
}
artifacts {
add("archives", sourcesJar)
add("archives", javadocJar)
}
}
我不仅需要添加testImplementation
库的源代码,还需要添加库的测试,以便在应用程序的测试中我能够使用库的测试包,而在应用程序的源代码中我将无法使用库的测试。
谢谢你。
更新:
基础构建:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.time.Duration
plugins {
kotlin("jvm") version "1.3.21"
}
allprojects {
repositories {
jcenter()
}
}
subprojects {
apply(plugin = "kotlin")
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.withType<Test> {
useJUnitPlatform()
// Make sure tests don't take over 10 minutes
timeout.set(Duration.ofMinutes(10))
}
}
基础设置:
import java.net.URI
rootProject.name = "base"
sourceControl {
gitRepository(URI("https://github.com/path-to-github.git")) {
producesModule("path.to.package:primitive-storage-layer")
}
}
include("library")
include("app")
应用程序构建:
plugins {
application
}
application {
mainClassName = "path.to.package.MainKt"
}
val junitVersion = "5.5.0-M1"
val hamkrestVersion = "1.7.0.0"
val mockkVersion = "1.9.3"
dependencies {
implementation(project(":library"))
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion")
testImplementation("com.natpryce:hamkrest:$hamkrestVersion")
testImplementation("io.mockk:mockk:$mockkVersion")
runtime("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
}
库.build:
val primitiveLibraryVersion = "1.0"
val gsonVersion = "2.8.5"
val junitVersion = "5.5.0-M1"
val mockkVersion = "1.9.3"
dependencies {
implementation("path.to.package:primitive-storage-layer:$primitiveLibraryVersion")
implementation("com.google.code.gson:gson:$gsonVersion")
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testImplementation("io.mockk:mockk:$mockkVersion")
}
解决方案
虽然可以生成一个包含测试的工件,然后从另一个项目中使用它,但我会避免这种情况,而是将测试实用程序重构为一个单独的子项目(类似于${app}-test-kit
),将代码放入主源集中,然后依赖它来自应用程序和库testImplementation
配置的子项目。
这为您提供了更清晰的分离,并且对于共享的内容和原因更加明显。
推荐阅读
- vb.net - 绑定数据网格不显示值
- c# - Record audio from two input device at the same time and save into wave file?
- node.js - 公牛竞技场表演公牛队列
- python-3.x - 确定开始参数 2D 高斯拟合
- mongodb - 如何在猫鼬模式中将对象 ID 数组设置为“必需”?
- react-native - 如何在 React-Native 中编写或导入 Bootstrap
- amazon-ecs - 多个任务的 AWS ECS 服务发现
- c# - 组合框不允许我写入和选择一个值
- ios - 如何查看内容 UITableView 的可复用池
- java - Java - 每行都缺少第一个字母