How to run Multiple Tests using TestNG XML File

In this tutorial, we will learn how to execute multiple tests in Selenium using a single configuration file named testng.xml file.

TestNG allow us to create multiple tests in a single suite. It helps to divide tests into different parts and grouping them based on modules, features, type of test (integration or unit), and so on.

So, let’s create multiple tests and execute them with the help of TestNG.xml configuration file.

1. Open Eclipse and create a new project named it “MultipleTest”.
2. Create a java class file named “FirstTestClass”. and add the following source snippet code onto it. 

Program source code: 1
package multipleTests; 
import org.testng.annotations.Test; 
public class FirstTestClass 
{ 
@Test 
public void firstTest() 
{ 
  System.out.println("First Test Method"); 
 } 
}

The preceding class “FirstTestClass” contains one test method “firstTest” which is annotated with @Test annotation. This test method will print a message onto the console when it will be executed.

3. Now, create another java class file named “SecondTestClass” and add the following source code onto it.

Program source code 2:

package multipleTests; 
import org.testng.annotations.Test; 
public class SecondTestClass 
{ 
@Test 
public void secondTest() 
{ 
   System.out.println("Second Test Method"); 
  } 
}


4. Now, create a testng.xml file and add the following snippet code as given below to it.TestNG XML File code:
<suite name="Suite"> 
<test name = "First Test"> 
<classes> 
      <class name = "multipleTests.FirstTestClass"/> 
</classes> 
</test> 
<test name = "Second Test"> 
<classes> 
      <class name = "multipleTests.SecondTestClass"/> 
</classes> 
</test> 
</suite>

The testng.xml file defines a suite with a name Suite. This suite contains two tests with the name “First Test” and “Second Test” respectively. These tests are configured to execute as separate classes multipleTests. FirstTestClass and multipleTests.SecondTestClass.

When we will execute this testng.xml file as a suite in TestNG, each class will be executed by a separate test section of suite.

5. Now, run testng.xml file in your eclipse. Once the testng.xml file will be executed, you will get the following output generated in Eclipse as shown in the below screenshot.

How to run multiple tests using testng xml file

We have successfully created a testng.xml file with multiple test sections in eclipse and then ran it using TestNG.

Now, in the next section, we will learn how to create and configure test suites to execute only tests belonging to a particular class, or a package or a particular test method.

How to create & configure TestNG Test Suite using Classes, Packages, & Test Methods


Before going ahead to create a test suite with classes, packages, and test methods, we will create a sample project first.

1. Open eclipse and create three packages, each package contains two classes, as shown in the following screenshot.

How to create and configure TestNG Test Suite using Classes, Packages, & Test Methods

2. Create two classes for each package as shown in the above screenshot and add the following source code of two test methods into each class.

@Test 
public void firstTest() 
{ 
   System.out.println("First test method"); 
 } 
@Test 
public void secondtTest() 
{ 
   System.out.println("Second test method"); 
}

When these test methods will be executed, they will print messages First test method and Second test method on the console.

How to create and configure TestNG Test Suite using Classes?


In this section, we will learn how to create and configure TestNG test suite using classes. We will use the sample project that we have created earlier. 



Create a new testng.xml file by name class-testng.xml to the project and add the following code snippet into created testng.xml file.
<suite name="Class Suite" verbose="1"> 
<test name = "Test"> 
<classes> 
      <class name = "firstPackage.FirstTestClass"> 
      <class name = "secondPackage.FirstTestClass"> 
      <class name = "thirdPackage.SecondTestClass"> 
</classes> 
</test> 
</suite>

In the preceding testng.xml file, we have defined a test by the name “Test” that contains three classes (one from each package).

Key points:
1. To add a class to your test suite, just use a combination of <classes> and </classes> tag as shown.

2. Inside <classes> tag, use class tag with attribute name having a value of class name along with package name (for example firstPackage.FirstTestClass) to add a test class to your test. You can use multiple class tags as per requirement under classes tag section. 

Now run the above class-testng.xml file using TestNG suite option in Eclipse. You will see the following result in eclipse as shown in below screenshot.

How to create and configure TestNG Test Suite using Classes

As you can observe in the previous screenshot, TestNG executed all test methods present inside the test class added to the test suite. But in this test suite, we have added a few test classes to the suite and executed them by excluding all other test classes present in the sample project.

How to create and configure TestNG Test Suite using Packages?


We know that a package may contain one or more classes in it. Using this configuration, we can add all classes under a package or its subpackage to the test suite.

In this section, we will use the same sample project as created earlier. Now, create a new testng.xml file by name package-testng.xml and place the following code to it.

<suite name="Package Suite"> 
<test name="Test"> 
<packages> 
       <package name = "firstPackage"/> 
       <package name = "secondPackage"/> 
</packages> 
</test> 
</suite>

In this testng.xml file, we have defined a test with two packages (firstPackage and secondPackage).

Key points:
1. To add a package to your test suite, just use a combination of <packages> and </packages> tags as shown.
2. Under <packages> tag, use package tag with attribute name having a value of package name (for example firstPackage) to add a package to your test.


Now, run the package-testng.xml file as a TestNG suite and observe the results in eclipse.

How to create and configure TestNG Test Suite using Packages

In the preceding testng.xml file, we have successfully created and executed a TestNG test suite by adding only two packages to a suite and excluded the third package in the project. As you can observe that TestNG executed all test methods inside test classes present in the packages.

In case if you want to execute all sub-packages inside a particular package, you will have to use .* ( dot star ) at the end of package name (as shown in the following code).

<suite name = "Package Suite" verbose = "1"> 
<test name = "Package Test"> 
<packages> 
     <package name = "test.* "/> 
</packages> 
</test> 
</suite>

This testng.xml file will execute all the sub-packages present under the main package “Test”.

How to create and configure TestNG Test Suite by Test Methods?


In this section, we will learn how to create and configure TestNG test suite by including a particular test method from test classes. We will use the above sample project created earlier for configuring test suite.

Now, create a new testng.xml file by name method-testng.xml to the project and add the following code snippet to it.

<suite name="Method Suite" verbose = "1"> 
<test name="Method Test"> 
<classes> 
   <class name = "firstPackage.FirstTestClass"> 
   <methods>
       <include name = "firstTest"/> 
   </methods> 
   </class> 
 </classes> 
</test> 
</suite>

In the preceding testng.xml file, we have used a tag <include>. TestNG provides two tags <include> and <exclude> to include or exclude certain test methods or packages from a set of tests.

Key point:
1. To configure TestNG test suite with test methods, we have to use tags methods and include/exclude under them to add or remove particular methods from a test class. Look at the testng.xml file.

Now, run testng.xml file as TestNG suite and look at the following results in Eclipse. In this way. we can execute a particular test method from a test class.

In case you would want to add more methods in XML file, you can use multiple <include> tags with the name of method that you want to include in the test execution.

How to create & configure Test Suite using Package, Class & Test Method?


In this section, we will create a TestNG test suite with all the combinations: package, class, and method in a single test.

Create a new testng.xml file by a name combine-testng.xml to your project and add the following source code given below.

<suite name = "Combine Suite"> 
<test name = "Combine Test"> 
<packages> 
     <package name = "firstPackage" /> 
</packages> 
<classes> 
      <class name = "secondPackage.FirstTestClass"/> 
      <class name = "thirdPackage.FirstTestClass"> 
   <methods> 
        <include name = "firstTest" /> 
   </methods> 
   </class> 
 </classes> 
</test> 
</suite>

Now, run this testng.xml file and look at the following results in eclipse. You can see the results as shown in the below screenshot.

How to create & configure Test Suite using Package, Class & Test Method

As you can observe in the combine-testng.xml file, we have created and configured a TestNG test suite by combining a particular package, class, and test method and then executed it.

This technique provides us the flexibility of creating  TestNG test suite with different packages, classes, and test methods depending on test requirements.

How to Include/Exclude Package in TestNG Test Suite?


TestNG provides us a convenient way to include or exclude tests while defining a TestNG test suite. It helps to define a particular set of tests in a test suite. For this, we will use <include> and <exclude> tags to include or exclude tests.

Let’s create and configure a test suite by including and excluding a test package in a test. We will take a sample project created earlier for the test.

Now, Create a new testng.xml file by a name include-exclude-testng.xml in the project and copy the following code to it.

<suite name = "Include-Exclude Suite"> 
<test name = "Include-Exclude Test"> 
<packages> 
    <package name = ".*"> 
    <include name = "firstPackage" /> 
    <exclude name = "secondPackage" /> 
 </package> 
</packages> 
</test> 
</suite>

The preceding test suite defines a test that includes all sub-packages of the main package “Test” (by .*).  But for test execution, we have included only a sub-package “firstPackage” by using a <include> tag with a name attribute value as package name, and by using <exclude> tag, we have excluded a particular subpackage “secondPackage”.

Now, run testng.xml file as TestNG suite in eclipse and see the following results of tests.

Final words
Hope that this tutorial has covered almost all important points related to how to run multiple tests using testng.xml file. I hope that you will have understood this topic and enjoyed coding.
Thanks for reading!!!

Next ⇒ Parallel Test Execution in Selenium using TestNG⇐ PrevNext ⇒

Leave a Comment