XML file configuration in TestNG framework
XML file is an important part of the TestNG framework
In this article we’re going to learn in detail all these points mentioned below:
- Introduction to XML file
- Purpose of an XML file
- Importance of XML file
- How to Create an XML file
- XML file structure
- Tag use in an XML file
- How to run an XML file
- Sample Code for XML file
- 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:
- First Right-click on the project → TestNG → Convert to TestNG
2. You will see a dialog box as shown in the picture then click on “Next”.
3. Then another dialog box will appear, click on “Finish”.
Then you will see an XML file under the project folder
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 structure
Here is the hierarchy of XML file that is structured at four levels:
Suite → Test → Class → Method
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
<suite name="Mobile App"> ... </suite>
OR
<suite name="Web App"> ... </suite>
OR
<suite name="Loan Department"> ... </suite>
OR
<suite name="Credit Card Department"> ... </suite>
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.
<suite name="Mobile App">
<test name="Add account">
...
</test>
</suite>
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:
<classes>
<class name="testcase.Signup"/>
<class name="testcase.Login"/>
</classes>
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.
...
<methods>
<include name="add"/>
</methods>
...
<exclude/> is used to exclude the only method from class that is define in this tag.
...
<methods>
<exclude name="addAccount"/>
</methods>
...
How to Run XML file
To run the XML file follow this step:
- Right-click on testng.xml file → Run As → TestNG Suite
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:
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 👍