TestNG provides different ways to run multiple testng.xml files
- Through code
- Through testng.xml itself
For this post let’s consider these below 2 testNG classes
TestNG1.java
package testng.MultipleRunTestNGXML; import org.testng.annotations.Test; import testngPractice.AfterBefore.BaseTest; public class TestNG1{ @Test public void Test1(){ System.out.println("Welcome to testNG1 : Test1"); } @Test public void Test2(){ System.out.println("Welcome to testNG1 : Test2"); throw new SkipException("SKip test"); } }
TestNG2.java
package testngPractice.MultipleRunTestNGXML; import org.testng.annotations.Test; import testngPractice.AfterBefore.BaseTest; public class TestNG2{ @Test public void Test1(){ System.out.println("Welcome to testNG2 : Test1"); int i = 2/0; } }
Now let’s create 2 testng.xml for the above classes
testng1.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="MySuite_login" parallel="none"> <test name="Login"> <classes> <class name="testngPractice.MultipleRunTestNGXML.TestNG1"/> </classes> </test> <!--test name="Theme"> <classes> <class name="testngPractice.AfterBefore.TestNG2"/> </classes> </test--> <!--Test > </Test--> </suite>
testng2.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="MySuite_settings" parallel="none"> <test name="Login"> <classes> <class name="testngPractice.MultipleRunTestNGXML.TestNG2"/> </classes> </test> <!--test name="Theme"> <classes> <class name="testngPractice.AfterBefore.TestNG2"/> </classes> </test--> <!--Test > </Test--> </suite>
for individual testng.xml file run we can right click on the testng.xml and run.
But let’s see how to run both the testng.xml files
Through code
We can use the TestNG class to run list of testng.xml files.
For this 1st we need to create a list of string which will hold all the testng.xml file paths we want to run
then will use testng.run() to run them sequencially
package testngPractice.MultipleRunTestNGXML; import org.testng.TestNG; import java.util.ArrayList; import java.util.List; public class RunMultipleXML { public static void main(String []args){ List<String> xmlFiles = new ArrayList<>(); xmlFiles.add("testng_Login.xml"); xmlFiles.add("testng_Settings.xml"); TestNG tng = new TestNG(); tng.setTestSuites(xmlFiles); tng.run(); } }
As this above is a main method, we can right click on the file and run
output –
You will get 2 sets of output as we have 2 xml files
Welcome to testNG1 : Test1 Welcome to testNG1 : Test2 =============================================== MySuite_login Total tests run: 2, Passes: 1, Failures: 0, Skips: 1 =============================================== Welcome to testNG2 : Test1 =============================================== MySuite_setting Total tests run: 1, Passes: 0, Failures: 1, Skips: 0 ==============================================
Throught testng.xml itself
We can create another testng.xml [can call as testng_master.xml file], which will hold all the individual testng.xml file paths inside it
then we can run the testng_master.xml in various ways
testng_master.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="MySuite" parallel="none"> <suite-files> <suite-file path="testng1.xml"/> <suite-file path="testng2.xml"/> </suite-files> </suite>
if you run above testng_master.xml, you will see the same output as above.
Thank you!