XML file configuration in TestNG framework

XML file is an important part of the TestNG framework

Fareena Imran
CodixLab
5 min readJan 28, 2022

--

In this article we’re going to learn in detail all these points mentioned below:

  1. Introduction to XML file
  2. Purpose of an XML file
  3. Importance of XML file
  4. How to Create an XML file
  5. XML file structure
  6. Tag use in an XML file
  7. How to run an XML file
  8. Sample Code for XML file
  9. Output by running XML file

Introduction to XML File

XML stands for “Extensible Markup Language”. TestNG uses an XML file to configure the test suite. The test suite is nothing but a collection of multiple test cases.

Purpose of XML File:

TestNG carries and stores data for multiple tests. It provides testers an easy way to run multiple test cases at only one click by defining their execution order.

Importance of XML File:

  • By using an XML file, you don't need to create a separate class for every single test case and run individual classes one by one as we do when working with JAVA.
  • What we need to do is just to define all the tests in one single class and then run these tests by using an XML file.
  • XML file is easy to read and use, you just need to define some tags.

How to Create an XML file

Here is a way to create an XML file in TestNG.

Steps:

  1. First Right-click on the project → TestNG → Convert to TestNG
Step to generate an XML file

2. You will see a dialog box as shown in the picture then click on “Next”.

Generate XML file
Step to generate XML File

3. Then another dialog box will appear, click on “Finish”.

Step to generate an XML file

Then you will see an XML file under the project folder

XML file

Double-tap on the testng.xml file, the XML file will open in a new tab on Eclipse IDE. So, in this way you can generate the XML file for your project. Here, is the XML file.

XML File

XML file structure

Here is the hierarchy of XML file that is structured at four levels:

Suite → Test → Class → Method

XML File structure
XML file structure

Tag use in the XML file

Here, are some tags that use in an XML file as some tags you can see in the above pictures.

All the tags start like <Tag_Name> and end like </Tag_Name>

OR some tags use like <Tag_Name />

1. <suite>

suite tag contains a collection of test tags. The suite can have one test tag or more than one test tag. You can give any name to the suite tag.

For example, if you are working at a company, there can be many departments, some maybe are working on a mobile app and some on a web app etc. then you can give suite name as on which department you are working like

2. <test>

A test tag is something like a module or folder where we define different classes or I can say test cases. you can give any name to the test tag. What type of module you are testing, write the name of that module in test tag

For example, if you’re testing a mobile app then there can be an option for add account for which you can create many test cases which will be defined in classes tag.

3. <Classes>

Classes tag are used to define one or more class tags. For example, if you have two test cases, one is for Signup.java and the second is for login. java then you can define these test cases in classes tags like:

4. <methods >

Methods tag are use to include or exclude methods from class. You need to define one more tag in this <methods> tag that are <include/> and <exclude/> Where;

<include/> is used to include the only method from class that is define in this tag.

<exclude/> is used to exclude the only method from class that is define in this tag.

How to Run XML file

To run the XML file follow this step:

  1. Right-click on testng.xml file → Run As → TestNG Suite
Run XML File

Sample Code for XML File

Here are 3 files Account.java, Calculator.java and testng.xml. Use these in your project, run the XML file and see the output.

Output By Running XML File:

Output

Hope this article has cleared your concepts about XML files, continue your learning about TestNG, follow all the articles step by step.

If you find this post useful, please tap 👏 button below 😊

Best of Luck 👍

--

--

Fareena Imran
CodixLab

SQA Engineer| Tech Writer | Write stories about SQA