TestNG Example: How To Create And Use TestNG.xml File

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated June 20, 2023

This Tutorial Explains How To Create TestNG.xml File With the Help of TestNG Example:

One of the most important topics of TestNG i.e. the TestNG.xml file will be explained in detail here.

A lot of tasks can be done simultaneously with the TestNG.xml file.

Let’s get started!!

=> Check Here To See A-Z Of TestNG Training Tutorials Here.

TestNG Example - TestNG.XML File

What Is TestNG.xml?

TestNG.xml file is a configuration file that helps in organizing our tests. It allows testers to create and handle multiple test classes, define test suites and tests.

It makes a tester’s job easier by controlling the execution of tests by putting all the test cases together and run it under one XML file. This is a beautiful concept, without which, it is difficult to work in TestNG.

Advantages Of TestNG.xml

Major advantages of TestNG.xml file are:

  • It provides parallel execution of test methods.
  • It allows the dependency of one test method on another test method.
  • It helps in prioritizing our test methods.
  • It allows grouping of test methods into test groups.
  • It supports the parameterization of test cases using @Parameters annotation.
  • It helps in Data-driven testing using @DataProvider annotation.
  • It has different types of assertions that help in validating the expected results with the actual results.
  • It has different types of HTML reports, Extent reports, etc. for a better and clear understanding of our test summary.
  • It has listeners who help in creating logs.

Concepts Used In TestNG.xml

#1) A Suite is represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.

Example: <suite name=”Testing Google Apps”>

#2) A Test is represented by <test> and can contain one or more TestNG classes.

Example: <test name=”Regression”>

#3) A Class is a Java class that contains TestNG annotations. Here it is represented by the <class> tag and can contain one or more test methods.

Example

<classes>
<class name="Googletest.GmailTest"/>
<class name="Googletest.MapsTest"/>
<class name="Googletest.ImagesTest"/>
</classes>

#4) A Test method is a Java method annotated by @Test methods in the source file.

Example:

public class GmailTest {
@Test
public void LoginTest()
{
System.out.println("Successfully Logged In");
}
@Test
public void LogoutTest()
{
System.out.println("Successfully Logged Out");
}
}

TestNG.xml Example

Basic Testng.xml file looks as shown below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Practice Suite">
  <test name="Test Basics 1">
	  <parameter name="emailid" value="tester456@gmail.com"/>
	  <parameter name="password" value="test@123"/>
	  <classes>
	  <class name="practiceTests.testParameters"/>
	  </classes>
  </test> <!-- Test -->	
  
  <test name="Test Basics 2">    
  	<parameter name="emailid" value="tester789@gmail.com"/>
    <classes>
       <class name="practiceTests.testOptional"/>
    </classes>
    
  </test> <!-- Test -->
</suite> <!-- Suite -->

Sample TestNG.xml file

Steps To Create TestNG.xml File

In TestNG, we have to create the TestNG.xml file to handle multiple test classes. We have to configure our test run, set test dependency, include or exclude any classes, test methods, packages, tests, etc. and set the priority as well in the XML file.

Let’s create the Testng.xml file using the below steps.

Step1: Right-click on the Project folder, go to New and select ‘File’ as shown in the below image.

Create the testng.xml file

Step 2: Add the file name as ‘testng.xml’ as shown in the below image and click on the Finish button.

Add file name as ‘testng.xml’

Step 3: Now you can add the below XML code in your testng.xml file. You can choose your Test suite name and the Test name as per the requirements.

Post providing the required information, the testng.xml file looks as below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Testing Google Apps">
    <test name="Regression">
         <classes>
             <class name="Googletest.GmailTest"/>
             <class name="Googletest.MapsTest"/>
             <class name="Googletest.ImagesTest"/>
          </classes>
     </test> <!-- Test -->
</suite> <!-- Suite -->

Look of the Testng.xml file

In the above XML file, you can see the sequence of tags properly and accurately. Suite => Test Classes => Class.

Here, the Suite name is <suitename=“Testing Google Apps”>

Test name is <testname=“Regression”>

We can give any name to the Suite and Test in the XML file. But we have to provide the correct name to the classes tag which is a combination of your Package name and the Test Case name.

Package name is Googletest and the test case names are:

<class name=“Googletest.GmailTest”/>
<class name=“Googletest.MapsTest”/>
<class name=“Googletest.ImagesTest”/>

Googletest Package

Step 4: Let’s run the xml file. Run the test by right clicking on the TestNG xml file and select Run As -> TestNG Suite.

TestNG Suite

Once the testng.xml file has run, we can see the results in the console.

Results in the console

Example Run Using TestNG.xml

Here, we have created the Suite name as <suitename=“Demo Suite” verbose=“1” > and the Test name as <testname=“Regression Test”>

We can give any name to the Suite and Test in the XML file. But we have to provide the correct name to the classes’ tag which is a combination of your Package name and Test Case name.

The package name is basicsDemo and the test case names are GoogleImages and GoogleMaps.

Sample Example run

Let’s run the XML file. Run the test by right click on the TestNG XML file and select Run As => TestNG Suite.

Once the testng.xml file has run, we can see the results in the console.

TestNG Results

Conclusion

We explored all about TestNG.xml in this tutorial. The various advantages and concepts used in TestNG.xml were explained in detail with the help of a TestNG Example

We hope you enjoyed the entire range of tutorials in this TestNG series.

Happy Reading!!

=> Visit Here For The Exclusive TestNG Training Tutorial Series.

Was this helpful?

Thanks for your feedback!