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

Output:-

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.

Output:-

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.

Table of Contents

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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.

TestNG Tutorials 66 : Sharing Data Among Test Methods in TestNG

by Amod Mahajan time to read: 2 min
1
%d bloggers like this: