How to Create a TestNG Test Suite to Run Multiple Test Cases

Print Page

The TestNG Test Suite is a collection of test cases that are run together via a “testng.xml” file. As a tester, you can sort the test cases to run in a particular order or perhaps have them dependent on a testcase or a group.

Steps to Create a TestNG Test Suite

Let’s now look at how we can create a “testng.xml’ file and use them in our project.

  1. Navigate to your project under Project Explorer panel > Right-click on project > New > Select “File”
  2. In “New File” wizard, type in “testng.xml” for file name and hit “Finish”
  3. The newly created test suite file is listed under Project Explorer panel

TestNG Test Suite

TestNG Test Suite Attribute

The testng.xml file has support for several attributes which you can use while running a test. We have used some of these in the examples shown after this list:

  • name: Name of Test Suite
  • verbose: You can change the test execution log details in console by specifying verbose level. Possible value is between 1-10.
  • parallel: Run parallel tests/classes/methods/suites
  • thread-count: Specifies the number of threads to be used when running parallel tests. This attribute is ignored if the parallel attribute is not enabled
  • annotations: Type of annotations used
  • time-out: Time out tests after a specific amount of time(milliseconds)

Example 1: Run Multiple Classes in a Test Suite

In this example we are running multiple classes from the same package by calling them in the suite.

File: Class1.java

package TestNGDemo;
import org.testng.annotations.Test;
public class Class1 {
	@Test
	public void TestA() {
		System.out.println("Test A");
	}
}
class Class2 {
	@Test
	public void TestB() {
		System.out.println("Test B");
	}
}

File: testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestSuiteDemo" verbose="1">
	<test name="Test1">
		<classes>
			<class name="TestNGDemo.Class1">
			</class>
			<class name="TestNGDemo.Class2">
			</class>
		</classes>
	</test>
</suite>

Similarly, we can also call classes from multiple packages. Just make sure to specify the package name and class name correctly in the file.

Example 2: Run multiple packages parallelly

In the below example there are two <test> tags defined. Each test has two specific packages and we have also used parallel attribute to execute them parallelly.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestSuiteDemo" parallel="tests" thread-count="2">
    <test name="Demo1">
        <packages>
            <package name="TestNGDemo" />
        </packages>
    </test>
 
    <test name="Demo2">
        <packages>
            <package name="TestNGDemo2" />
        </packages>
    </test>
</suite>

On running this script, TestNG will execute both packages together and look into them for methods and classes to run. Do note that the methods and classes inside the package are still run one after the other.

Example 3: Run All Tests in a Package Except a Group

In this example, we are running all methods and classes in TestNGDemo2 package, except those grouped in donotrun.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestSuiteDemo">
    <test name="Demo1">
        <groups>
            <run>
                <exclude name="donotrun" />
            </run>
        </groups>
        <packages>
            <package name="TestNGDemo2" />
        </packages>
    </test>
</suite>

These are some very simple examples to show the usage of TestNG Test Suite. You can add on these and use this feature in your projects to execute hundreds of test cases automatically.