如何使用gradle测试将命令行参数传递给测试?
我正在使用gradle来运行JUnit测试。问题是我需要将参数从命令行传递给测试。我尝试传递系统属性但失败了。
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
这是我的测试:
public class MyTest {
@Test
public void someTest() throws Exception {
assertEquals(System.getProperty("arg1"), "something");
}
}
失败,因为没有 arg1
参数。
是否有可能以某种方式传递命令行参数?
It fails because there is no arg1
argument.
Is it possible somehow to pass command line arguments?
当你运行 gradle test时-Darg1 = smth
,将系统参数 arg1
传递给gradle jvm,而不是测试运行测试的jvm。它的设计是为了保护测试免受副作用的影响。
When you run gradle test -Darg1=smth
, you pass system parameter arg1
to gradle jvm, not test jvm where tests are run. It is designed this way to protect tests from side effects.
如果你需要使用param来进行测试,请使用像这样的smth
If you need to propagete param to tests, use smth like this
test {
systemProperty 'arg1', System.getProperty('arg1')
}
并以相同的方式运行