Execute multiple classes from testng xml.

Before we start with this article, one should know what is testng.xml? what are the content of Testng.xml.
Below is recommended article to understand about testng.xml

What is TestNG.xml?

Now, let us talk about running multiple classes via testng.xml file.
Our test cases will be in different classes, we can run multiple classes from testng.xml
Let us take some example.
I have created 2 packages having different classes in it. Each class have various tests in it.

Let us see how can we run all these in single testng.xml file.
Class1 of Package1: 

package package1;

import org.testng.annotations.Test;

public class Class1 {
@Test
public void Package1Class1Test1(){
System.out.println("Package1Class1Test1 is executed");
}
@Test
public void Package1Class1Test2(){
System.out.println("Package1Class1Test2 is executed");
}

}
Class2 of Package1:

package package1;

import org.testng.annotations.Test;

public class Class2 {

@Test
public void Package1Class2Test1(){
System.out.println("Package1Class2Test1 is executed");
}
@Test
public void Package1Class2Test2(){
System.out.println("Package1Class2Test2 is executed");
}
}

Class1 of Package2:

package package2;

import org.testng.annotations.Test;

public class Class1 {

@Test
public void Package2Class1Test1(){
System.out.println("Package2Class1Test1 is executed");
}
@Test
public void Package2Class1Test2(){
System.out.println("Package2Class1Test2 is executed");
}

@Test
public void Package2Class1Test3(){
System.out.println("Package2Class1Test3 is executed");
}

}

Now, we can create testng.xml file.
In testng.xml, under tag <classes> We can add multiple classes, so that all classes will run.
Below is testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="example suite 1" verbose="1" >
<test name="Test suite 1" >
<classes>
<class name="package1.Class1"/>
<class name="package1.Class2"/>
<class name="package2.Class1"/>
</classes>
</test>
</suite>

 

Below console output shows, all the classes mentioned in testng.xml has run.
Console Output:
[TestNG] Running:
  C:UsersPrakashworkspacesampleProjecttestng.xml
Package1Class1Test1 is executed
Package1Class1Test2 is executed
Package1Class2Test1 is executed
Package1Class2Test2 is executed
Package2Class1Test1 is executed
Package2Class1Test2 is executed
Package2Class1Test3 is executed
===============================================
example suite 1
Total tests run: 7, Failures: 0, Skips: 0
===============================================
Hope this helps !!!!

 
 

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories