简体   繁体   中英

How can I override a property in an integration test from the command line in a gradle project?

Say, for example, you have a data source specified in a properties file and you want to override it in CI. If I do something like:

gradle -DdataSource.user=fred clean integrationTest

...will this be visible to the running JVM of the test or will this go only to Gradle? What's the right way to do this? I'm actually writing a test right now to figure this out, but somebody else can have the cheddar if the answer shows up here first.

No, just passing it this way won't make this variable avalable in your tests. You have configure your test tasks to make them populate this property into tests.

This could be done once for all the test tasks this way

tasks.withType(Test) { systemProperty 'datasource.user', System.getProperty('datasource.user', 'defaultisername') }

Or if you have a number of variable to populate, then pass them all once, like so

test {
    options {
        systemProperties(System.getProperties())
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM