TestNG ITestContext to store data and share among the TestNG class

Sameera De Silva
2 min readOct 30, 2019

--

Assume that you want to share a variable called attendance among your class only. In your methods in the class you want to assign the value initially and retrieve it and change it as you go by.

One way to achieve this in Java by using a static variable , but since static variables have global scope, other classes can access it directly .

To avoid this we can use TestNG ITestContext.

it’s uses Key and value pair mechanism, you can define a key and it’s corresponding value .

setAttribute() to store variable
getAttribute() to get variable

Let’s see the sample code that I wrote to demonstrate this .

package com.webdriver.blogs;import org.testng.ITestContext;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class ItestContextStoreAndShareDataAmongTestngTestMethods {
static int attendence = 0;
/*
* Using ITestContext , we can store and share data across the tests in a same
* TestNG class. setAttribute("key","value") to store variable using a Key
* getAttribute("key") to get variable using the Key
*
* Note: All the @Test methods that we are going to run must be in one class
* only.
*
*/
@BeforeClass()
public void startFootballMatch(ITestContext context) {
System.out.println("Number of Fans at the stadium " + attendence);
// let's use ITestContext to store a variable called number of Policemen
int policemenCount = 50;
context.setAttribute("Poliemen", policemenCount);
System.out
.println("Number of Poliemen at the stadium stored in key Poliemen and count is " + policemenCount);
}
@Test(priority = 1, enabled = true, description = "print attendence before interval")
public void attendenceBeforeInterval(ITestContext context) {
System.out.println("Number of Fans at the stadium befor interval " + attendence);
// let's get the stored value from ITestContext directly
System.out.println("Number of Poliemen at the stadium before interval stored in key Poliemen and count is "
+ context.getAttribute("Poliemen"));
}@Test(priority = 2, enabled = true, description = "print attendence after interval")
public void attendenceAfterInterval(ITestContext context) {
System.out.println("Number of Fans at the stadium after interval " + attendence);
// let's get the stored value from ITestContext and assign it to a variable
int policeMen = (int) context.getAttribute("Poliemen");
System.out.println("Number of Poliemen at the stadium after interval stored in key Poliemen and count is : "
+ policeMen);
}
@AfterClass()
public void shutDown(ITestContext context) {
// we can change the value of Key in context so after the match reduce the count
// to 5
context.setAttribute("Poliemen", 5);
int policeMen = (int) context.getAttribute("Poliemen");
System.out.println("Number of Poliemen at the stadium after the match : " + policeMen);
}}

Output

[RemoteTestNG] detected TestNG version 6.14.3Number of Fans at the stadium 0Number of Poliemen at the stadium  stored  in key Poliemen and count is   50Number of Fans at the stadium befor interval  0Number of Poliemen at the stadium before interval stored  in key Poliemen and count is   50Number of Fans at the stadium after interval  0Number of Poliemen at the stadium after interval stored  in key Poliemen and count is :   50Number of Poliemen at the stadium after the match :   5

Please see the free Video URL — https://youtu.be/ozzXbppx08o

Please follow the Author — https://www.linkedin.com/in/sameera-de-silva-11247922/

--

--

No responses yet