TestNG XML example to execute Multiple Classes

In testng.xml file we can specify multiple name (s) which needs to be executed.

In a project there may be many classes, but we want to execute only the selected classes.

We can pass class names of multiple packages also. If say suppose, we want to execute two classes in one package and other class from some other package.

The below is the example testng.xml which will execute the class names that are specified.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="example suite 1" verbose="1" >
  <test name="Regression suite 1" >
    <classes>
      <class name="com.first.example.demoOne"/>
      <class name="com.first.example.demoTwo"/>
      <class name="com.second.example.demoThree"/>
    </classes>
 </test>
</suite>

We need to specify the class names along with packages in between the classes tags.

In the above xml, we have specified class name as “com.first.example.demoOne” and “com.first.example.demoOne” which are in “com.first.example” package. And class name demoThree is in package “com.second.example.”

All the classes specified in the xml will get executes which have TestNG annotations.

Below are the two example classes under “com.first.example” package which is executed.

Package: “com.first.example”
Classname: demoOne

package com.first.example;
import org.testng.annotations.Test;
public class demoOne {
	@Test
	public void firstTestCase()
	{
		System.out.println("im in first test case from demoOne Class");
	}
	
	@Test
	public void secondTestCase()
	{
		System.out.println("im in second test case from demoOne Class");
	}
}

Classname: demoTwo

package com.first.example;
import org.testng.annotations.Test;
public class demoTwo {
	@Test
	public void firstTestCase()
	{
		System.out.println("im in first test case from demoTwo Class");
	}
	
	@Test
	public void secondTestCase()
	{
		System.out.println("im in second test case from demoTwo Class");
	}
}

Package: “com.second.example”
Classname: demoThree

package com.second.example;
import org.testng.annotations.Test;
public class demoThree {
	@Test
	public void firstTestCase()
	{
		System.out.println("im in first test case from demoThree Class");
	}
	@Test
	public void secondTestCase()
	{
		System.out.println("im in second test case from demoThree Class");
	}
}

We need to run the testng.xml file. (Right click on testng.xml and select Run as ‘TestNG Suite”)
The below is the output for the above example code.

TestNG with multiple classes

Test Frameworks: 

Comments

Hi,

I want to try execute two classes at a time but when i declared as "public static WebDriver cd1" in one class
and i wrote some code and again i am trying to try code in another class while am write code with object(cd1) it is asking to declare object but i am already object declared as public also getting error why.

@Anubhav - absolutely correct. This has solved my problem

Hi

I am executing a test suit which is having 5 classes 1 is login and remaining are modules after the login.
When I run the test suit it is opening 1 browser window for login class and another 4 browser windows for 4 classes? Is it possible to execute all classes in 1 browser window? If yes I can achieve that?

I have prepared the script like
public class Testcase1
@BeforeClass
public void openApp()
{
}
@BeforeMethod
public void login()
{
}
@AfterMethod
public void logOut()
{
}
@AfterClass
public void closeApp()
{
}
@Test(priority=1)
public void first()
{
}
@Test(priority=2)
public void second()
{
}

it has to execute before class,before method,first(),second(),after method,after class right?
but it is running like before class,before method,first(),after method,after class...but skipping the second().
how to make it run?

Can someone please tell me the basis on which multiple classes containing multiple Test cases will be created in a given project.

I want to run some multiple classes in order using xml
how to give priority to class names

for xml file should i create it in new package or can create in com.first.example package?

It’s a good article. Thank you so much. i am developing a hybrid framework of an application where around 10 modules and when we mouse hover on it it gives 5-6 options/link/new page after clicking each of it i have to a write a script for it. Say example User Management module having -4 option EditProfile page, Chang Password Page, AboutPage and LogOut page. Same way each of module have same way options/link or page. How do i create testNg.xml file. Should i create individual package for each module? and execute each module/package . Just give me an idea how should i create my testng.xml file. Please reply will be a great help to me.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.