How to set an environment variable with the JVM Test Suite plugin

I have an integration test task that’s set up via the JVM Test Suite plugin. I need to add an environment variable to this task. Previously, I would do this:

tasks.register('integrationTest', Test) {
    if (!environment.get("FOO")) {
        environment.put("FOO", "bar")
    }
    ...
}

I tried to add the if block to the integrationTest(JvmTestSuite) closure, but evidently environment is unknown at that point. I could use a hammer and do

tasks.withType(JavaExec).configureEach {
    if (!environment.get("FOO")) {
        environment.put("FOO", "bar")
    }
}

but that seems like it’d be overkill. Is there a different way to set the variable just for the integration test task that I’m missing?

Your hammer would be broken anyway, as the task is of type Test, not JavaExec :smiley:

You could also get the task using its name and configuring it.

But what you actually are after is within the test suite’s block

targets.all {
    testTask {
        environment("FOO", "bar")
    }
}

I’m also not sure the if makes much sense to be honest.
I don’t think it will ever be set already.

Can you be more specific about “within the test suite’s block”? I tried within testing.suites.test.integrationTest, but in both cases Gradle said

Could not find method testTask() for arguments [build_e35hwv745lktfqdti46lzmcxr$_run_closure10$_closure38$_closure41$_closure45$_closure46@1d26da3a] on object of type org.gradle.api.plugins.jvm.internal.DefaultJvmTestSuiteTarget.

For more completeness, here’s the full testing closure:

{
    suites {
        configureEach {
            dependencies {
                implementation "org.spockframework:spock-core"
                implementation platform("org.spockframework:spock-bom:2.4-M1-groovy-4.0")
            }
        }

        test {
            useJUnitJupiter()

            dependencies {
                implementation project()

                implementation "org.spockframework:spock-spring"
                implementation "org.apache.groovy:groovy-json:4.0.13"

                // Mock webserver
                implementation "com.squareup.okhttp3:okhttp"
                implementation "com.squareup.okhttp3:mockwebserver"
            }
        }

        integrationTest(JvmTestSuite) {
            dependencies {
                implementation "com.tomtom.http:goji-http-client:3.3.0"
            }

            targets.all {
                testTask {
                    if (!environment.get("FOO")) {
                        environment.set("FOO", "bar")
                    }
                }
            }
        }
    }
}

Ah, I already tought you that actually: Configure logging with the JVM Test Suite Plugin - #3 by spartanhooah :slight_smile:

Ah, that’s true. However, the below targets.all closure doesn’t seem to set the environment variable:

targets.all {
    testTask.configure {
        if (!environment.get("FOO")) {
            environment.set "FOO", "bar"
        }
    }
}

I have the below test class setup method and it’s printing null for the value of FOO:

def setupSpec() {
    println("FOO: ${System.getenv('FOO')}")
}

Well, hard to guess from here what is strange in your build.
This works exactly like it should, I just tried.