Skip to content

Gradle

class Gradle

Control center for Gradle TestKit. See gradle.

Properties

dir

val dir: Prepared<Path>

A temporary directory unique for each test, in which the Gradle files are created.

Example

Print the directory:

test("In which directory does Gradle execute?") {
    println(gradle.dir())
}

properties

suspend fun Gradle.properties(text: String)

Helper function to write the gradle.properties file.

Example
test("Configure the JVM heap") {
    gradle.properties("""
        org.gradle.jvmargs=-Xmx3g -Xms200m
    """.trimIndent())
}

Accessor for the gradle.properties file.

Example
test("Access the gradle.properties file") {
    println(gradle.properties())
}

rootProject

Accessor for the files of the root project.

Example

Create the root build.gradle.kts file:

test("Create the root build.gradle.kts file") {
    gradle.rootProject.buildKts("""
        println("Configuring the project")
    """.trimIndent()
}

See also

settingsGroovy

suspend fun Gradle.settingsGroovy(text: String)

Helper function to write the settings.gradle file.

Example
test("Create a groovy settings file") {
    gradle.settingsGroovy("""
        println "Loading the settings…"
    """.trimIndent())
}

See also

Accessor for the settings.gradle file.

Example
test("Access the groovy settings file") {
    println(gradle.settingsGroovy())
}

See also

settingsKts

suspend fun Gradle.settingsKts(text: String)

Helper function to write the settings.gradle.kts file.

Example
test("Create a Kotlin DSL settings file") {
    gradle.settingsKts("""
        println("Loading the settings…")
    """.trimIndent())
}

See also

Accessor for the settings.gradle.kts file.

Example
test("Access the Kotlin DSL settings file") {
    println(gradle.settingsKts())
}

See also

Functions

project

fun project(path: String): Project

Accessor for the files of a project, given its path.

Example

Create the modules/foo/build.gradle file:

test("Configure the :modules:foo project") {
    gradle.project("modules/foo").buildGroovy("""
        println "Configuring the project!"
    """.trimIndent())
}

See also

runner

suspend fun runner(): GradleRunner

Instantiates a GradleRunner in dir.

Examples
test("Create the root build.gradle.kts file") {
    gradle.rootProject.buildKts("""
        tasks.register("print") {
            doLast {
                println("Configuring the project")
            }
        }
    """.trimIndent()

    val result = gradle.runner()
        .withPluginClasspath()
        .withArguments("print")
        .build()

    result.output shouldContain "Configuring the project"
}

See also

  • GradleRunner.withPluginClasspath: When writing tests for a plugin, automatically adds it to the executed Gradle instance

  • GradleRunner.withArguments: Specify which tasks should be executed

  • GradleRunner.build: Executes the build, expecting a success

  • GradleRunner.buildAndFail: Executes the build, expecting a failure