How to obtain tasks configured via jvm-test-suites plugin

Hello There,

I am currently migrating a project from gradle-testsets-plugin to the jvm-test-suites plugin (using gradle 8.1.1).

I have the demand to obtain the test task configured via test-suites for the docker-compose plugin

testing {
    suites {

        register<JvmTestSuite>("componentTest") {
            dependencies {
                implementation(...)
                ...
            }
        }      
    }
}
val componentTest by tasks.named<Test>("componentTest")

dockerCompose {    
    isRequiredBy(componentTest)
    ...
}

This worked without problems with the test-sets plugin but now the gradle build fails with

FAILURE: Build failed with an exception.

* Where:
Build file '.../build.gradle.kts' line: 226

* What went wrong:
Task with name 'componentTest' not found in root project 'foobar'.

Commenting out those lines and running gradle tasks clearly states that the task exists.

Verification tasks
------------------
check - Runs all checks.
componentTest - Runs the component test suite.
...

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

Is that a bug?

Thanks,
Sebastian

Okay I found a workaround. Maybe that helps anyone else.

Instead of using the dockerCompose plugin configuration block

dockerCompose {    
    isRequiredBy(componentTest)
    ...
}

you can configure the test suite target tasks like this

testing {
    suites {

        register<JvmTestSuite>("componentTest") {
            ...

            targets {
                all {
                    testTask {
                        dependsOn(tasks.named("composeUp"))
                        finalizedBy(tasks.named("composeDown"))
                    }
                }
            }
        }
    }
}

Can you share an MCVE?
Because this works fine here:

testing {
    suites {
        register<JvmTestSuite>("componentTest")
    }
}

println("FOO")
val componentTest by tasks.named<Test>("componentTest")
println(componentTest)
println("BAR")

Btw. the idiomatic construct would be val componentTest by tasks.getting(Test::class).

But either way it prevents task configuration avoidance for the task though as you eagerly realize it, so if isRequiredBy also accepts a task provider you should change to give it a task provider instead using = instead of by.

Hi Björn,

thanks for your reply I think the problem is the order of the statements. This works

testing {
    suites {
        register<JvmTestSuite>("componentTest")
    }
}

val componentTest by tasks.named<Test>("componentTest")

This does not work:

val componentTest by tasks.named<Test>("componentTest")
testing {
    suites {
        register<JvmTestSuite>("componentTest")
    }
}

I assumed that the order does not play a role due to the configuration phase but I am not a gradle pro.

So the solution to me is to declare the dockerCompose block after the testing block.

Both these statements are done at configuration time and tests.named fails if the task is not present already.
So yes, order matters in this case, it would be like using a variable before declaring it, which also does not work in most programming languages. :slight_smile: