How to run TestNG XML file programmatically

Posted on

TestNG is a popular testing framework in Java, often used for automation testing. Usually, we run TestNG tests using testng.xml in tools like Eclipse or IntelliJ. However, sometimes we need to execute the testng.xml file programmatically, especially in frameworks or custom execution setups.


In this blog, we'll see how to do that in simple steps.

Why run testng.xml programmatically?


Running TestNG programmatically allows us to:

  • Control test execution dynamically
  • Run multiple testng.xml files from a Java class
  • Integrate TestNG with other automation tools or CI/CD pipelines

Steps to run testng.xml programmatically


Step 1: Add testNG dependency

If you are using Maven, add this dependency in pom.xml:

XML
Copy
<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.9.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Step 2: Create a Test class


Create a simple test class with @Test annotation.

Java
Copy
import org.testng.annotations.Test;

public class SampleTest {
    @Test
    public void testMethod() {
        System.out.println("TestNG Test is running!");
    }
}

Step 3: Create a testng.xml file


XML
Copy
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MySuite">
    <test name="MyTest">
        <classes>
            <class name="SampleTest"/>  
        </classes>
    </test>
</suite>

Step 4: Run testng.xml programmatically


Now, let's write Java code to execute testng.xml.

Java
Copy
import org.testng.TestNG;
import java.util.Arrays;

public class RunTestNGXML {
    public static void main(String[] args) {
        TestNG testng = new TestNG();
        
        // Provide the path of testng.xml file
        testng.setTestSuites(Arrays.asList("testng.xml"));
        
        // Run TestNG
        testng.run();
    }
}

Output

When you run RunTestNGXML.java, you will see output like:


TestNG Test is running!
PASSED: testMethod
===============================================
MySuite
Total tests run: 1, Failures: 0, Skips: 0
===============================================