How To Create the TestNG.XML File in Selenium?

Author: Timothy Joseph
Timothy Joseph | June 13, 2022

How To Create the TestNG.XML File in Selenium?

Inspired by NUnit and JUnit, TestNG is a type of java testing open-source framework in selenium. It provides more powerful features than JUnit and is easy to use. NG stands for next-generation which means new features and functionalities that TestNG brings with it. TestNG supports all ranges of testing such as simple unit testing or be it complex integration testing. It helps to simplify all testing requirements such as functional testing, regression, or end-to-end testing. It is widely used by testing companies and is stimulated by JUnit Java and NUnit .Net platforms.

Below Is How We Can Create a TestNG.XML File

To create or maintain multiple test cases we require a TestNG.XML file. The main prerequisites are that we need to configure our test run, set the test dependency, include/exclude any test, set priority, and create a class or package in the file.

  1. Right-click on the project folder → go to New and then select ‘File’.

    Create a TestNG.XML File

  2. In the New file wizard, add the filename as ‘testing XML’ and click on the Finish button.
  3. After this, it will add an XML file below the project folder.
  4. Write the below code:

    <suite name="Testing">
      <test name="Regression">
        <classes>
          <class name="Testing.Regression"/>
       </classes>
      </test>
    </suite>

    The suite is signified by one XML file. It can comprise one or more tests.

    A Test can hold one or more TestNG classes.

    A Class is defined as a Java class that comprises TestNG annotations.

    It can contain more than one test method.

    Classes tag is named as -> a combination of the Package name and the Test Case name.

    A Test method is a Java method annotated as @Test in the source file.

**User can choose any name for Test Suite & Test Name**

 

Example of TestNG.XML File

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Practice">
 <test name="Test1">
     <parameter name="Username" value="user123@gmail.com"/>
     <parameter name="password" value="test@123"/>
     <parameter name="mobile_number" value="1234567899"/>
     <classes>
       <class name="Practice.Test1"/>
     </classes>
 </test><!-- Test -->
</suite><!-- Test -->

TestNG framework is widely used by many software companies that use selenium because of its multiple ranges of advantages and features. This is very comfortable and easy to use. Test cases can be easily created using annotations in TestNG. It supports data-driven testing. It supports parameterization as well. We can create a group of test cases, and execute them based on priority. It supports parallel execution and generates logs that are of real help. No need for inheritance property. It allows us to define dependent test cases. Any name can be given to methods without any constraint of naming.

Disclaimer

This publication is for informational purposes only, and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.

Post a Comment