我在TestNG中使用套件文件来定义我想要运行的测试。这些套件是通过jenkins工作触发的,现在我需要将它作为可选项来排除特定组。
我想在jenkins中添加一个额外的构建参数,并在系统属性中添加一个标志,如果此参数设置为如此-DexcludeMyGroup=true
。在我的基础测试中的一些@BeforeSuite
或@BeforeTest
方法中,我想检查属性及其值。取决于我想从我的套件中排除该组。
我试过了
@BeforeTest
public void beforeTest(XmlTest test) {
if (!Boolean.parseBoolean(System.getProperty("excludeMyGroup"))) {
test.addExcludedGroup("myGroup");
}
}
以及
@BeforeSuite
public void beforeSuite(ITestContext context) {
if (!Boolean.parseBoolean(System.getProperty("excludeMyGroup"))) {
cont.getSuite().getXmlSuite().addExcludedGroup("myGroup");
}
}
但两者都不起作用。
我试图使用第二种方法修改其他参数,如线程计数,这使用cont.getSuite().getXmlSuite().setThreadCount(10)
工作正常,但我还没有找到一种方法来排除套件文件以外的特定组。之后是否有可能排除这种情况?
我发现了几种方法:
public static void main(String[] args) {
TestNG tng = new TestNG();
tng.setExcludedGroups("excludedGroup1, excludedGroup2");
tng.run();
}
然后你可以从终端运行类文件,然后就可以了
$ java <classfilename> excludedgroup1 excludedgroup2
并编写主要功能如下:
public static void main(String[] args) {
TestNG tng = new TestNG();
tng.setExcludedGroups(args[0] + ", " + args[1]);
tng.run();
}
-excludegroups
的命令行开关,它将以逗号分隔的要排除的组列表:http://testng.org/doc/documentation-main.html#running-testng。