Package execution using TestNG xml

Let us consider a scenario where you want to execute all the test case of all the classes in a package. This can easily be done via TestNG.xml file. In TestNG.xml we can include a package or packages.

Below is the sample of TestNG.xml file to run all the test cases of a package.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Package Execution Suite">
  <test name="Package Execution">
  <packages>
  	<package name="com.qatechhub.testng.package1"></package>
  </packages>
  </test>
</suite>

Here, in above testng.xml file a package with name com.qatechhub.testng.package1 is included. Let us consider this package consist of two different classes with name TestClass1 and TestClass2.

After executing testng.xml file all the test cases of these above-mentioned classes will be executed.

Code of TestClass1:

package com.qatechhub.testng.package1;

import org.testng.annotations.Test;

public class TestClass1 {
	@Test
	public void tc1(){
		System.out.println("Executing testcase1 of testclass1..");
	}
	
	@Test
	public void tc2(){
		System.out.println("Executing testcase2 of testclass1..");
	}
	
	@Test
	public void tc3(){
		System.out.println("Executing testcase3 of testclass1..");
	}

}

Code of TestClass2:

package com.qatechhub.testng.package1;

import org.testng.annotations.Test;

public class TestClass1 {
	@Test
	public void tc1(){
		System.out.println("Executing testcase1 of testclass2..");
	}
	
	@Test
	public void tc2(){
		System.out.println("Executing testcase2 of testclass2..");
	}
	
	@Test
	public void tc3(){
		System.out.println("Executing testcase3 of testclass3..");
	}

}

Result of above execution:

packageExecution

HTML Report of above code:

report

In this way we can execute test cases of all the classes in a package or multiple packages.

PS: For any questions queries or comment feel free to write us at saurabh@qatechhub.com or support@qatechhub.com. Happy Learning 🙂

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: