Sharing data between @Test methods
For a given class, sharing data between @Test methods can be done via means of Class level variables
class MyTestNGClass
{
String CustName=”Jackychan”;
// This can be used across @Test methods normally
}
Suppose our Tests spans across classes, ITestContext interface can come handy as per example below:
Step 1: your test classes:
——————————-
package com.TestNG.Package;
import org.testng.ITestContext;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class MyClass1 {
@BeforeSuite
public void Setup(ITestContext context) {
String CustID = “Jackychan”;
context.setAttribute(“CustID”, CustID);
}
@Test
public void MyFunction(ITestContext context) {
System.out.println(context.getAttribute(“CustID”));
}
}
package com.TestNG.Package;
import org.testng.ITestContext;
import org.testng.annotations.Test;
public class MyTestNGClass2 {
@Test
public void MyFunction(ITestContext context) {
System.out.println(context.getAttribute(“CustID”)+”Hello”);
}
}
Step 2: Create your test suite using testng.xml :
————————————————–
Output:
——–
[RemoteTestNG] detected TestNG version 6.14.3
Jackychan
JackychanHello
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
0 comments