What is TestNG.xml and How to use it?

QAble Testlab Private Limited
3 min readJul 1, 2021
What is TestNG

What is TestNG.xml ?

In TestNG framework, we need to create Testng xml file to create and handle multiple test classes. We do configure our test run, set test dependency, include or exclude any test, method, class or package and set priority etc in the xml file.

TestNG.xml file is an XML file which contains all the Test configuration and this XML file can be used to run and organize our test.

TestNG eclipse plugin can be used to automatically generate testng.xml file so no need to write from scratch. In this article, we are going to learn about what is testng.xml and how to use testng.xml file to run our test.

TestNG support two kinds of parameterization, using @Parameter+TestNG.xml and using@DataProvider. In @Parameter+TestNG.xml parameters can be placed in suite level and test level.

If the Same parameter name is declared in both places; test level parameter will get preference over suit level parameter.

You understand how vital a TestNG XML file is for running parallel tests in a Selenium Grid, it is time to learn how to create a TestNG XML file. There are two methods to create a TestNG XML file, feel free to use either of them-

Method 1:

  • Right-click on the project folder & towards the bottom of the list, select TestNG and then “Convert to TestNG.”
  • Click on Next
  • The next window that pops up will have the refactored source code, which would be applicable after you click Finish.
  • A new addition to your project directory would be displayed named as testng.xml.
  • Double click on testng.xml to open the xml file

Method 2:

  • Right-click on Project, then New, and finally select File.
  • Next, enter the following file name “testng.xml” and then click on Finish.
  • In this step, you will have to enter the XML code manually. That is how the two methods differ from each other. After entering the XML code, you can select a name for your liking suite and class. This is what a basic XML file looks like-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="testng.TestNGTestOne"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Now you can copy the above code (with your choice of names) on the TestNG XML file we’ve created above.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Cycle1">
<test name = "Tutorialspoint">
<classes>
<class name = "Test1" />
<methods>
<exclude name= “Login.*”/>
</methods>
</classes>
</test>
</suite>

The testng.xml file has the numerous uses as listed below

  • Test cases are executed in groups.
  • Test methods can be included or excluded in the execution.
  • The execution of multiple test cases from multiple java class files can be triggered.
  • Comprises names of the folder, class, method.
  • Capable of triggering parallel execution.
  • Test methods belonging to groups can be included or excluded in the execution.

How to use TestNG

The hierarchy of an XML file is quite straightforward. It goes something like this- first < suite > tag, second < test > tag and lastly, < classes > tag.

As mentioned above, you can give any name to the suite and test tag, but you have to be careful while naming classes since it is a combination of the Package name and Test name.

Since suite is the topmost hierarchy, this is where multiple test classes can be placed.

For example, we have three test classes, one for opening Google, another for searching on Google, and one to open Gmail. All these three classes can be defined under the same test tag for execution.

The next step is to run the project using the XML file. Right-click on the testng.xml file and navigate to “Run As >> TestNG Suite.”

You can see for yourself that the test classes mentioned in the XML ran successfully, and with it, you can now create your own test suite and run multiple tests all in one go and subsequently get the results.

In our upcoming posts, we will learn about TestNG annotations along with the examples. Stay tuned and Happy TestNG!

--

--

QAble Testlab Private Limited

QAble: A leading software testing company. We offer various software testing services to help you to achieve the best quality of your web & mobile application.