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

Oleksandr :

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?

AdamSkywalker :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=455458&siteId=1