Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give System property to my test via Gradle and -D

People also ask

How do I pass project property in Gradle?

Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command. You can also set system properties in gradle. properties files with the prefix systemProp.

How do I run a test using Gradle command?

You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.


The -P flag is for gradle properties, and the -D flag is for JVM properties. Because the test may be forked in a new JVM, the -D argument passed to gradle will not be propagated to the test - it sounds like that is the behavior you are seeing.

You can use the systemProperty in your test block as you have done but base it on the incoming gradle property by passing it with it -P:

test {
    systemProperty "cassandra.ip", project.getProperty("cassandra.ip")
}

or alternatively, if you are passing it in via -D

test {
    systemProperty "cassandra.ip", System.getProperty("cassandra.ip")
}

Came across this very much problem, except i don't want to list all properties given on the commandline in the gradle script again. Therefore i send all system properties to my test

task integrationTest(type: Test) {
    useTestNG()
    options {
        systemProperties(System.getProperties())
    }
}

I had a case where I needed to pass multiple system properties into the test JVM but not all (didn't want to pass in irrelevant ones). Based on the above answers, and by using subMap to filter the ones I needed, this worked for me:

task integrationTest(type: Test) {
    // ... Do stuff here ...
    systemProperties System.getProperties().subMap(['PROP1', 'PROP2'])
}

In this example, only PROP1 and PROP2 will be passed in, if they exist in gradle's JVM.


Here's a variant that passes numerous project properties to the test JVM as system properties. I prefer project properties over system properties to increase flexibility.

task intTest(type: Test) {
    systemProperties project.properties.subMap(["foo", "bar"])
}

Which may be passed on the command-line:

 $ gradle intTest -Pfoo=1 -Pbar=2

And retrieved in your test:

String foo = System.getProperty("foo");

Here is something that worked for me

//in build.gradle file

    tasks.withType(Test) {
        systemProperties = [
           ip: System.getProperty('ip', '192.168.33.13'),
        ]
    }

    task integrationTests(type: Test){
        useTestNG()
    }

Suppose if you are using TestNG, you can add the annotation @Parameters as shown below

  public class IpAddress {
    @Test
    @Parameters("ip")
    public void printIpAddress(String ip) {
        System.out.println(ip);
    }
 }

Now you are good to execute a gradlew command

./gradlew clean -Dip="xx.xx.xx.xx" integrationTests --tests "IpAddress"

If you want to use @DataProvider to pass the test data, you can pass it like below and execute the same above gradle command to run the test

 public class IpAddress {
    @DataProvider(name = "GetIP")
    private static Object[][] getIp() {
        return new Object[][]{
                //if -Dip is not provided in command, then by default it gets the value assigned in build.gradle file i.e.'192.168.33.13'
                {System.getProperty("ip")}, 
        };
    }

    @Test(dataProvider = "GetIP")
    public void printIpAddress(String ip) {
        System.out.println(ip);
    }
}