Source:Handling InterruptedException in Java
@BeforeSuite
public void setUpConfiguration() {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
Document doc = null;
DocumentBuilder docBuilder;
try {
docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.parse("D:\\PLAYGROUND\\demo\\src\\main\\resources\\testSuites\\testng.xml"); // path to your testng.xml
NodeList parameterNodes = doc.getElementsByTagName("test");
System.out.println("Total Number of tests to be executed are : " + parameterNodes.getLength());
} catch (ParserConfigurationException |SAXException | IOException e) {
System.out.println("Exception while reading counting tests in testng.xml");
}
}