TestContext
is being initialized automatically by TestNg. You can access it from your test methods which are listed below (for doing that you need to specify the method parameter in its signature):
For some details you can refer to this post.
And here is my example of using TestConext with running tests programmatically:
public class Main {
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { Main.class });
testng.addListener(tla);
testng.run();
}
@BeforeClass
public void setUp(ITestContext cntx){
cntx.setAttribute("option1", new String("My Option 1"));
cntx.setAttribute("option2", new String("My Option 2"));
}
@Test
public void testTest(ITestContext cntx){
System.out.println(cntx.getAttribute("option1"));
System.out.println(cntx.getAttribute("option2"));
}
}