TestNG Tutorials 67 : Sharing Data Among Test Methods in TestNG Using ITestContext

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

In previous post, We have learnt sharing data among test methods using static variables. But this is not optimal. I see below problem in using static variable approach:-

  1. You may need to create multiple static variables.
  2. Dynamic handling of variables is not easy.
  3. Problems in parallel execution
  4. Scope of variables

All above problems can be solved using ITestContext interface. As per TestNG Javadoc, ITestContext interface defines a test context which contains all the information for a given test run. An instance of this context is passed to the test listeners so they can query information about their environment.

ITestContext is a powerful interface which provides many useful methods. In this post, we will see two important methods setAttribute​(java.lang.String name, java.lang.Object value) and getAttribute​(java.lang.String name).

setAttribute(atttributeName, attributeValue) :- Set a custom attribute. It is similar to adding at element in a Map as key-value pair. Kindly pay attention here that attribute value can be of any type. This is the reason this method accepts Object type as value.

getAttribute(attributeName) :- Get the value of given attribute name. Remember return type is an Object.

Above two methods belong to IAttributes interface. ITestContext interface extends IAttributes interface. In stead of going theoretically, let’s learn it using example. It will make more sense to you.

ITestContext reference is created once and can be used with @Test annotated method by just passing it as a parameter.

package TestNGExamples;

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

public class SharingStateBetweenTestsUsingITestContext {
	// ITestContext reference is created once for the duration of test run.
	// 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);
	}
	
	
	@Test
	public void useData(ITestContext context)
	{
		String lastName = "Mahajan";
		context.setAttribute("LastName", lastName);
		// Retrieving attribute value set in ITestContext
		String FN = (String) context.getAttribute("FirstName");
		String fullName = FN +" "+lastName;
		System.out.println("Full Name is : "+fullName);
		context.setAttribute("FullName", fullName);
	}

}

Output:-

Full Name is : Amod Mahajan

You can see there is no need to create any variable in advanced. ITestContext gives a container in which we can store labeled values and use wherever required. It works fine as well when we need to use these variables in other TestNG class as below:-

package TestNGExamples;

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

public class SharingStateBetweenTestsUsingITestContext2 {
	
	@Test
	public void useDataInOtherClass(ITestContext context)
	{
		String profession = "Blogger";
		String fullName = (String) context.getAttribute("FullName");
		String nameWithProfession = fullName +" "+profession;
		System.out.println("Full Name is : "+fullName);
		System.out.println("Full Name with profession is "+ nameWithProfession);
	}

}

Since we need to run multiple TestNG classes together, create a XML as below:-




  
    
      
      
    
   
 

Output:-

Full Name is : Amod Mahajan
Full Name is : Amod Mahajan
Full Name with profession is Amod Mahajan Blogger

Only constraint is that you need to pass ITestContext reference in @Test annotated method if you want to use to store or retrieve variables. No changes are required in style of running TestNG class if you use ITestContext.

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

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

1 thought on “TestNG Tutorials 67 : Sharing Data Among Test Methods in TestNG Using ITestContext

  1. ITestContext is an interface and it is implement by TestRunner,but in code we does not discuss on TestRunner class how it works or internal working

Leave a Reply

Your email address will not be published. Required fields are marked *