Carle B. Navy
Carle B. Navy

Reputation: 1156

Exclude groups in TestNG not in suite

I am using a suite file in TestNG to define the tests I want to run. Those suites are triggered with a jenkins job and now I need to make it optional to exclude a specific group or not.

I thought about adding an additional build parameter in jenkins and add a flag to the system properties if this parameter is set like so -DexcludeMyGroup=true. In some @BeforeSuite or @BeforeTest method in my base test I want to check for the property and its value. Depending on that I want to exclude that group from my suite.

I have tried

@BeforeTest
public void beforeTest(XmlTest test) {
  if (!Boolean.parseBoolean(System.getProperty("excludeMyGroup"))) {
      test.addExcludedGroup("myGroup");
  }
}

as well as

@BeforeSuite
public void beforeSuite(ITestContext context) {
  if (!Boolean.parseBoolean(System.getProperty("excludeMyGroup"))) {
      cont.getSuite().getXmlSuite().addExcludedGroup("myGroup");
  }
}

but both do not work.

I have tried to use the second approach to modify other parameters such as thread count and this works fine using cont.getSuite().getXmlSuite().setThreadCount(10) but I have not yet found a way to exclude a specific group besides the suite file. Is there a possibility to exclude this afterwards?

Upvotes: 1

Views: 1520

Answers (1)

Emily
Emily

Reputation: 82

I found a couple ways to do this:

  1. You can also run a TestNG suite programmatically in the main method, and use command line strings to define what groups to exclude (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(); }

Then you could run the class file from the terminal and simply do

$ java <classfilename> excludedgroup1 excludedgroup2

and write the main function as the below:

public static void main(String[] args) {
    TestNG tng = new TestNG();
    tng.setExcludedGroups(args[0] + ", " + args[1]);
    tng.run();
}
  1. TestNG has a command-line switch called -excludegroups that will take a comma separated list of the groups you want to exclude, if you run the testng.xml file from the command line: http://testng.org/doc/documentation-main.html#running-testng.

  2. Run it through Maven's surefire plugin. Go to the "excluded groups" part of this page -- you can define them in the pom.xml this way.

Upvotes: 1

Related Questions