不在套件中排除TestNG中的组

问题描述 投票:0回答:1

我在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)工作正常,但我还没有找到一种方法来排除套件文件以外的特定组。之后是否有可能排除这种情况?

java jenkins testng
1个回答
1
投票

我发现了几种方法:

  1. 您还可以在main方法中以编程方式运行TestNG套件,并使用命令行字符串来定义要排除的组(http://static.javadoc.io/org.testng/testng/6.11/org/testng/TestNG.html#setExcludedGroups-java.lang.String-): 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();
}
  1. 如果从命令行运行testng.xml文件,TestNG有一个名为-excludegroups的命令行开关,它将以逗号分隔的要排除的组列表:http://testng.org/doc/documentation-main.html#running-testng
  2. 通过Maven的surefire插件运行它。转到此页面的“排除组”部分 - 您可以通过这种方式在pom.xml中定义它们。
© www.soinside.com 2019 - 2024. All rights reserved.