How to pass command line arguments to tests with gradle test?

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())
    }

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.