1

I have a TestRunner class that starts my TestNG like this:

TestRunConfigs configs = TestRunConfigs.parseCommandLine(args);

TestNG testRunner=new TestNG();
testRunner.setXmlSuites(getXmlSuites(configs.TestSuites));
testRunner.run();

I need to provide some values to all tests in all my suites. I think ITestContext is the right way to do that. I just don't know where to do that. Any directions ?

2

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

enter image description here

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"));
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.