Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 68 : Sharing Data Between Tests in a Suite Using ISuite & ITestContext

Posted on 01/13/2025 By admin

As a part of TestNG Tutorials, in this post we will see “How to share data or state among test in a suite?”.

We have covered below topics as of now:-

Sharing data between @Test annotated method using static variable concept

Sharing data between @Test annotated method using ITestContext

Let’s understand what actually I want to cover in this post. I will create two TestNG classes. In TestNG XML, I will create two tests and will add one TestNG class to each test. I would like to use data created in first test into second test. We will use concept of ITestContext. See example below:-

First TestNG Class:-

package TestNGExamples;

import org.testng.ITestContext;
import org.testng.annotations.Test;

public class SharingDataSuiteLevelFirstTest {
        
        // Passing ITestContext as a parameter to @Test method to use it. 
        @Test
        public void generateData(ITestContext context)
        {
                String firstName = "Amod";
                // Setting an attribute with name and its value
                context.setAttribute("FirstName", firstName);
        }
}

Second TestNG Class:-

package TestNGExamples;

import org.testng.ITestContext;
import org.testng.annotations.Test;

public class SharingDataSuiteLevelSecondTest {
        
        // Passing ITestContext as a parameter to @Test method to use it. 
        @Test
        public void useData(ITestContext context)
        {
                String lastName = "Mahajan";
                // Retrieving attribute value set in ITestContext
                String FN = (String) context.getAttribute("FirstName");
                String fullName = FN +" "+lastName;
                System.out.println("Full Name is : "+fullName);
        }
}

TestNG XML ( Note here that I have created two tests) :-



            

Output:-

Full Name is : null Mahajan

Observe output now. We were expecting output as “Amod Mahajan” but it printed “null Mahajan”. Actually scope of added variable in ITestContext is within current test. Outside of test, it will give you null.

Solution is again ITestContext but this time we need to store data at suite level in stead of a test level. ITestContext interface provides a method named getSuite() which returns a Suite object that was passed to the runner at start-up i.e. ISuite. Since ISuite interface extends IAttribute interface, you get setAttribute() and getAttribute() methods. So we need to get suite first and then set and get values.

Modified TestNG classes are as below:-

Modified first TestNG class:-

package TestNGExamples;

import org.testng.ISuite;
import org.testng.ITestContext;
import org.testng.annotations.Test;

public class SharingDataSuiteLevelISuiteFirstTest {
        
        // Passing ITestContext as a parameter to @Test method to use it. 
        @Test
        public void generateData(ITestContext context)
        {
                String firstName = "Amod";
                // Get the suite
                ISuite suite = context.getSuite();
                // Setting an attribute with name and its value at suite level in stead of test level
                suite.setAttribute("FirstName", firstName);
        }
}

Modified second TestNG class:-

package TestNGExamples;

import org.testng.ISuite;
import org.testng.ITestContext;
import org.testng.annotations.Test;

public class SharingDataSuiteLevelISuiteSecondTest {
        
        // Passing ITestContext as a parameter to @Test method to use it. 
        @Test
        public void useData(ITestContext context)
        {
                String lastName = "Mahajan";
                // Retrieving attribute value set in ITestContext
                ISuite suite = context.getSuite();
                String FN = (String) suite.getAttribute("FirstName");
                String fullName = FN +" "+lastName;
                System.out.println("Full Name is : "+fullName);
        }
}

TestNG XML:-



            

Output:-

Full Name is : Amod Mahajan

Note:-

  1. You can not pass ISuite reference directly to @Test annotated method like ITestContext. If you do so, you get a TestNG exception stating Cannot inject @Test annotated Method [generateData] with [interface org.testng.ISuite].
  2. You can store attributes at test and suite level together as required.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Frequently Asked Java Program 06: Print a Floyd Triangle of given number of rows
Next Post: REST Assured Tutorial 58 – What is JsonPath and how to create it for simple and nested JSON Object?

Related Posts

image – Make Selenium Easy Uncategorized
October 5, 2017 – Make Selenium Easy Uncategorized
API Testing Tutorial Part 5 – Safe Methods in HTTP Methods Uncategorized
make selenium easy selenium webdriver tutorials Uncategorized
Baby Steps To Become Efficient Selenium-Java Automation Tester Uncategorized
IMG_6689[1] – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark