【问题标题】:Use code from test package in different module's tests在不同模块的测试中使用测试包中的代码
【发布时间】:2019-09-12 23:38:21
【问题描述】:

我在 kotlin 中有一个使用 gradle dsl 配置的“基础”项目。 在基地内部,我有模块“应用程序”和模块“库”。 模块应用程序使用模块库。 我想从模块应用程序的测试中访问模块库的测试(准确地说是库的 testUtils 包)。

app.build gradle 文件有 implementation(project(":library")) 行,我无法在我的主代码中访问库的测试,这很好,但我也无法从应用程序的测试中访问它。

我读过this questionthis question 的可能副本),其中相关的 groovy 代码是:

task testJar(type: Jar, dependsOn: testClasses) {
    baseName = "test-${project.archivesBaseName}"
    from sourceSets.test.output
}

configurations {
    tests
}

artifacts {
    tests testJar
}

但我无法在 gradle dsl 中实现它。

我还检查了this question 的代码在哪里:

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,不仅是库的源代码,还有库的测试,以便在应用程序的测试中我能够使用库的测试包,而在应用程序的源代码中我将无法使用库的测试.

谢谢。

更新:

base.build:

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))
    }
}

base.settings:

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")

app.build:

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")
}

library.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")
}

【问题讨论】:

  • 你能分享你当前的 gradle 文件吗?
  • @dbramwell 添加了 gradle 文件

标签: gradle-kotlin-dsl


【解决方案1】:

虽然可以生成一个包含测试的工件,然后从另一个项目中使用它,但我会避免这种情况,而是将测试实用程序重构为一个单独的子项目(类似于${app}-test-kit),将代码放入 main 源集,然后从应用程序和库 testImplementation 配置中依赖该子项目。

这使您可以更清晰地分离,并且更清楚地了解共享的内容和原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多