Running TestNG tests from a Java main Class

Here is the source code example that shows how to execute the TestNG tests using main method. In below example, TestNG tests from org.softpost.Class1 and org.softpost.Class2 classes will be executed.

package org.softpost;

import org.testng.TestNG;
import org.testng.xml.XmlSuite;

public class MainClass {
    public static void main(String[] args) {

        TestNG testngRunner = new TestNG();
        //To run tests in Parallel mode
        testngRunner.setParallel(XmlSuite.ParallelMode.METHODS);
        //Provide the list of test classes
        testngRunner.setTestClasses(new Class[] {
                org.softpost.Class1.class,org.softpost.Class2.class });
        //Run tests
        testngRunner.run();
    }
}

Here is the output of above code.

[TestNG] Running:
Command line suite

Test2 from Class2
Test1 from Class2
Test2 from Class1
Test1 from Class1

===============================================
Command line suite
Total tests run: 4, Failures: 1, Skips: 0
===============================================

You may also like...