top of page
anupriyak2050

TestNG XML

TestNG.xml is a configuration file to organize and execute tests in TestNG. This xml file contain all the test configurations and can be used to run and manage the test. We can configure our test run, set test dependencies, include or exclude any test, method, class or package and set priority in the XML file.


Methods to create a TestNG XML file:

There are two methods to create a TestNG XML file:

Method 1:

Right-click on the project folder, select 'TestNG' and then select 'Convert to TestNG'. Once you click the popup, a new configuration tab will appear with ‘Refactoring’ source code and then you click Finish. Once testng.xml file is created successfully, you can find it under your project directory.


testng.xml file creation

Method 2:

Right-click on Project, then click New, and finally select File. Next, enter the file name “testng.xml” and then click on Finish.

New------>others------->File------>next----->testng.xml----->Finish


How to run test cases using the testng.xml file?

The testng.xml file use different keywords and configurations to execute and organize test cases.The hierarchy among the testng.xml is as follows:

•suite

•test

•packages

•classes

•methods


Suite in TestNG is represented by <suite> tag, and can have multiple attributes passed. It can contain one or more tests.

  • Test in TestNG is represented by <test> tag and can contain one or more TestNG classes.

  • Classes in TestNG is represented by <classes> tag and holds a list of objects.

  • Class of TestNG is a Java class that contains at least one TestNG annotation and can contain one or more test methods.

  • Methods in TestNG is represented by <methods> tag. We can use include or exclude methods.


*We have to define xml comments between <!-- and -->. Also, we have to create XML file with .xml extension.


Example of testng.xml file:

<?xml version="1.0" encoding="UTF-8"?> <!--XML version and Encoding type-->

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<!--'Document Type Definition' title for TestNG   --->                                

<!--Dtd has validated elements and attributes that are defined inside the Dtd document.---->

<suite name="BankSuite" > ---------->Suite is the top level of element of testng.xml.

 <test thread-count="8" name="CardTest" > -------> An integer giving the size of the thread pool to use

 <groups>

  <run>

   <include name="card"/>

   </run>

  </groups>

<classes>

  <class name="com.bank.cardTests"> ------->packagename.classname

  <methods>

   <include name="credit.*"/> ------> Include method using regular expression displayed only "credit" related testcase

  </methods>

  </class>

  </classes>

  </test>

  <test thread-count="5" name="LoanTest">

  <classes>

  <class name="com.bank.LoanTests">

  <methods>

   <exclude name="carLoan"/> -------->Exclude method skip the Test case

   </methods>

  </class>

  </classes>

 </test> <!-- Test -->

</suite> <!-- Suite -->


Packages:

<suite name="Suite">

  <test thread-count="5" name="Test">

  <packages>

   <package name ="testngexample"/> -------->packagename

  </packages>

  </test> <!-- Test -->

</suite> <!-- Suite -->


Parameters

Parameters in TestNG allow you to pass values to test methods at runtime. Parameters are defined in the <suite> or <test> level of testng.xml file.

Sample testng.xml with parameter:

<parameter name="url" value="https://opensource-demo.orangehrmlive.com/"/>


Groups

Groups allow to categorize the test methods. We can use groups to selectively execute specific set of tests. We can specify which groups to include or exclude during test execution.Filtering which test methods to run.


Sample testng.xml with groups:

<groups>

<run>

<include name="card"/>

</run>

</groups>


Data Providers

A Data Provider is a method in TestNG that return collection of objects to use as parameters for a test method. Data provider returns an two dimensional array of object. There are two parameters supported by DataProvider are Method and ITestContext.


Syntax:@Test(dataProvider=“DataProviderName”)


Example of DataProvider:

@Test(dataProvider="getData")

public void t2(String username,String password) //passing parameter

{

System.out.println("Test2 Method");

}


The test method linked to the data provider.

@DataProvider

    public Object[][] getData(){

        return new Object[][] {

            { "First username", "password" },

            { "Second username", "password" },

            { "Third username", "password" }

        };

}


Listeners

TestNG allow you to specify listeners in the XML file. You can use <listeners> tag to attach listeners to your suite or test level. Listeners in TestNG are used to listen and respond to different events during test execution life cycle (test start, test pass, test fail). Listeners are used for logging, screenshots and generating reports. ITestListener listens to individual test method execution (start, pass, fail and skipped).


Example of Listener program:

package com.bank;

import org.testng.ITestListener;

import org.testng.ITestResult;

public class MyTestListeners implements ITestListener {

@Override

public void onTestStart(ITestResult result) {

System.out.println("Test Started:"+result.getName());

}

@Override

public void onTestSuccess(ITestResult result) {

System.out.println("Test Passed:"+result.getName());

}

@Override

public void onTestFailure(ITestResult result) {

System.out.println("Test Failed:"+result.getName());

}

@Override

public void onTestSkipped(ITestResult result) {

System.out.println("Test skipped:"+result.getName());

}

}


Example of Listener testng.xml

*Right click on testng.xml file and then run as TestNG Suite.



Advantage of TestNG.xml:

Test Organization:

TestNG XML allow us to organize tests efficiently, making it easier to manage and maintain test suites. We can create dependency of one test method on another test method.


ParallelTest Execution:

TestNG XML support parallel execution of tests, allow us to run multiple tests at a same time. We can configure tests to run in parallel across different threads, classes or methods.


Cross-browser Testing:

We can define multiple browsers in the XML file enabling selenium tests to run on different browsers without editing the test code. It helps in prioritizing our test method.


Parameterization:

TestNG xml allow you to pass parameter of your test methods such as browser name, url, username and password. It also supports the parameterization of test cases using @Parameters annotation.


DataProviders:

It helps in data-driven testing using @DataProvider annotation. Data providers are very useful for parameterized testing allowing you to run a single test method multiple times with different sets of data.


Integration CI/CD

TestNG xml file works well with continuous integration tools like Jenkins allowing you to trigger the test cases.


Conclusion

TestNG XML is an important feature for organizing and configuring your test cases. It is a popular and most widely used testing framework for Selenium automation. We can use multiple ways to write the test cases in proper order. It also helps with parallel execution, include and exclude method, groups, parameterization and report generation.



34 views

Recent Posts

See All
bottom of page