Running testNG test without XML FILE using TestNG CODE

43 views Asked by At

I have test ready but I can't run test because I am working in normal java but for running multiple test I am using TestNG but I don't know how to run multiple test using TestNG code and also I don't know how to map it with listener class.

I have tried below code :

TestNG test = new TestNG() ;
test.setTestClasses(new Class[] {class name.class}) ;

test.run() ;

But I don't now for multiple test and for parallel test how to proceed further and also I don't know how to map listeners class to it. Please help me to deal with this

1

There are 1 answers

0
sashkins On

XML file is much more convenient and much easier to read/handle/reuse/etc.

If you are really sure you want to run the test programmatically, then I suggest you read the official documentation about how to do it.

It all depends on your exact needs. From the question, the approximate code to run multiple tests (assuming test classes) with listeners may look like the following:

public static void main(String[] args) {
    TestNG testng = new TestNG();

    // parallel settings
    testng.setParallel(XmlSuite.ParallelMode.CLASSES);
    testng.setThreadCount(3);

    // add classes
    testng.setTestClasses(new Class[] { TestClass1.class, TestClass2.class, TestClass3.class });

    // add listeners
    testng.addListener(new Listener1());
    testng.addListener(new Listener2());

    // run
    testng.run();
}

The official documentation contains an example with Suite and Test objects, in case you want to have them.