TestNG Tutorials 66 : Sharing Data Among Test Methods in TestNG

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

It might be confusing for you that what I meant by “Sharing states or data among test methods“. So let me explain this first.

Suppose in a TestNG class, you have two @Test annotated methods. In first method you have created some data and same data you want to use in second method. This is called sharing data between tests. We can do in multiple approaches. General way to use a static variable to store and retrieve as required. We will see an example below:-

package TestNGExamples;

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

public class SharingStateBetweenUsingStaticVariable {
	
	// Creating a static variable so that updated value can be used across run
	public static String fullName = "";
	
	// Priority is provided to run test method in a specific sequence
	@Test(priority =1 )
	public void generateData()
	{
		String firstName = "Amod";
		// Setting the data
		fullName = firstName;
	}
	
	
	@Test(priority =2 )
	public void useData(ITestContext context)
	{
		String lastName = "Mahajan";
		// Modifying the data updated by first method
		fullName = fullName +" "+lastName;
		System.out.println("Full Name is : "+fullName);
	}

}

Output:-

Full Name is : Amod Mahajan

Above you can see how method “useData” used data generated in “generateData”.

There may be another scenario as well. In above scenario, both @Test method were in a same class. What in the situation when you want to use data of one TestNG class in to another TestNG class i.e. @Test annotated methods are in different classes ? In similar fashion as above, we can use as below:-

package TestNGExamples;

import org.testng.annotations.Test;

public class SharingStateBetweenUsingStaticVariable2 {
	
	
	@Test(priority =1 )
	public void useDataFromOtherClass()
	{
		String profession = "Blogger";
		// Modifying the data updated by second method
		SharingStateBetweenUsingStaticVariable.fullName = SharingStateBetweenUsingStaticVariable.fullName +" "+profession;
		System.out.println("Full Name with profession is : "+SharingStateBetweenUsingStaticVariable.fullName);
	}	

}

Since we need to run both TestNG classes as a suite, create a testng xml as below. If you run above testng class directly, it will not get the updated value of variable.




  
    
      
      
    
   
 

Run above xml as observe ouput.

Output:-

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

So, here is the normal way generally maximum people opt to share data among tests. In next posts, we will see more optimal way to do the same.

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 66 : Sharing Data Among Test Methods in TestNG”

  1. Thanks for sharing it.I got Very valuable information from your blog.your post is really very Informative. Iā€™m satisfied with the information that you provide for me.Selenium is powerful. It will support different language.. Different platform.Selenium is a open source. Multi user can use at a time.Selenium also supports for mobile testing. Simulators like Ipad, Iphone and android phone we can test using selenium. So Selenium people are in demand now.By reading your blog, i get inspired and this provides some useful information.

Leave a Reply

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