This question already has an answer here:
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?
When you run
gradle test -Darg1=smth
, you pass system parameterarg1
to gradle jvm, not test jvm where tests are run. It is designed this way to protect tests from side effects.If you need to propagete param to tests, use smth like this
and run it the same way
Use -D to send your parameters in. Like so:
See the gradle command line documentation of -D.
To access it in the tests, you need to propagate it in your build.gradle file.
You can also pass all System Properties like so: