Make Selenium Easy

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:-


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:-

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.


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.

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.

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

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

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.