I have a gradle script that in the test
section sets a lot of testing environment variables. These testing variables are used across different packages and I would like to extract this into a separate gradle file that I could have all the variables in a single place.
Currently I have something like
test {
environment('XXXX', System.getenv("XXXX"))
....
}
I attempted to extract all this into a single gradle script at the root like so:
ext.setupTestEnv = {
environment('XXXX', System.getenv("XXXX"))
....
}
and then call it like:
test {
useJUnitPlatform()
apply from: '../../gradle/testEnvSetup.gradle'
setupTestEnv()
}
However, this failed I think because environment
can only be called from test. I tried a couple other things but was unable to figure out how to actually setup the environment variables in setupTestEnv
. I was wondering how I might be able to set/unset the environment variables from a root gradle script so that I can maintain them in a single place.
Inside the test
block you are configuring an object of type Test
. So you can write a function in your script to accept such an object, eg:
ext.setupTestEnv = (Test task) -> {
task.environment('XXX', 'YYY')
}
Then, with your script code having been called earlier in a build script, you can call it to configure a specific Test
task in that build script:
test {
setupTestEnv(it)
}