17

I am using gradle to run JUnit tests. The problem is that I need to pass arguments from the command line to tests. I tries to pass System properties but failed.

gradle test -Darg1=something

Here is my test:

public class MyTest {
    @Test
    public void someTest() throws Exception {
        assertEquals(System.getProperty("arg1"), "something");
    }
}

It fails because there is no arg1 argument. Is it possible somehow to pass command line arguments?

Oleksandr
  • 3,574
  • 8
  • 41
  • 78

2 Answers2

22

When you run gradle test -Darg1=smth, you pass system parameter arg1 to the Gradle JVM, not the test JVM where tests are run. It is designed this way to protect tests from side effects.

If you need to propagate parameters to tests, use something like this

test {
    systemProperty 'arg1', System.getProperty('arg1')
}

and run it the same way.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
  • where should I put the test{ systemProperty 'arg1', System.getProperty('arg1')} script? I get this error: "could not find method test for arguments()" – Apperside Nov 20 '17 at 13:50
  • 1
    @Apperside check this https://github.com/cuba-platform/cuba/blob/master/build.gradle#L391 – AdamSkywalker Nov 20 '17 at 13:59
  • @ Apperside Put this text in the build.gradle – Big Al Mar 26 '19 at 21:37
  • how do you set a default value that can be overriden from command line? – red888 Aug 18 '20 at 15:16
  • When I add test method linke mentioned above in my gradle file, I get error saying "Could not find method test() for arguments on extension 'android' of type", is this updated recently? – krishna5688 Dec 02 '20 at 18:28
  • This works for me on gradle kotlin dsl! Change build.gradle `tasks.test { systemProperty("arg1", System.getProperty("arg1") ?: "default value") }` And then pass the argumet on cli: `./gradlew build test -Darg1=value` – Bruno Pereira May 28 '21 at 04:27
20

Use -D to send your parameters in. Like so:

./gradlew test -Dgrails.env=dev -D<yourVarName>=<yourValue>

See the gradle command line documentation of -D.

To access it in the tests, you need to propagate it in your build.gradle file.

    test {
       systemProperty "propertyName", "propertyValue"
    }

You can also pass all System Properties like so:

    test {
        systemProperties(System.getProperties())
    }
ninnemannk
  • 1,296
  • 1
  • 14
  • 26
  • I misread the OPs question, and made my edits before I refreshed the page to see your answer. – ninnemannk Feb 27 '17 at 18:19
  • He also has "all System Properties" option, which is missing from your answer. – zeusalmighty Jun 08 '18 at 16:12
  • Could you then use `System.getProperties('yourVarName')` in your Java code to obtain the `yourValue` passed by command line? – payne Jun 09 '20 at 21:34
  • When I add test method linke mentioned above in my gradle file, I get error saying "Could not find method test() for arguments on extension 'android' of type", is this updated recently? – krishna5688 Dec 02 '20 at 18:28