TestNG | Modify testng.xml file programmatically

At times, we may need to modify testng.xml programmatically before the test suite execution starts, for example by adding a listener in the background or exclude a test case with certain group tag.

TestNG provides the IAlterSuiteListener interface to implement a hook to capture the testng.xml instance before the test suite executes. When we get the XmlSuite instance, we can add or remove attributes as needed.

Copy
public class TestNGListener implements IAlterSuiteListener {
    
    @Override
    public void alter(List<XmlSuite> suites) {
        for(XmlSuite suite:suites) {
            suite.addIncludedGroup("bdd_runner");
      }
   }
}