将参数传递给TestNG运行 - Gradle

时间:2012-05-08 15:42:24

标签: java spring testng gradle

我在TestNG中有一些测试,我使用Gradle来运行它们。我在spring bean中有一些属性,我想在运行时选择它。

因此,为了选择这个不同的bean,我想将字符串参数传递给useTestNG调用。我的build.gradle看起来如下:

apply plugin: 'java'
dependencies {
  compile fileTree(dir: 'lib', include: '*.jar')
}
test {  
    useTestNG()
}

参数将类似于“bean1”,“bean2”,...并且在Java代码中我将选择使用bean。

    ApplicationContext context = new FileSystemXmlApplicationContext("**/pathToXML.xml");       
    Locators obj = (Locators) context.getBean("bean1");

如果这不是一个解决这个问题的好方法,不同的方法会更好我会很幸运地知道它

3 个答案:

答案 0 :(得分:1)

您可以传递所有系统属性:

test {
    systemProperties System.getProperties()
    useTestNG()
}

答案 1 :(得分:0)

最好包括将参数从testng本身传递给应用程序代码。 如果从gradle传入不同的参数,则会阻止从其他位置(如Eclipse等IDE)轻松快速地执行单元测试。

答案 2 :(得分:0)

您可以按如下方式在测试任务中传递系统属性:

test{
    systemProperty 'bean1', 'com.foo.Bar'
    useTestNG()
}

在Java代码中,您可以使用System.getProperty("bean1");

访问它