Running TestNG Tests from Java Main Method

Filed Under: TestNG

TestNG tests are executed automatically when we build a maven project. We can also run TestNG tests using Eclipse plugin. What if we don’t have the TestNG plugin installed for our IDE and we want to run some specific tests without doing a complete build. In this case, we can run TestNG test classes from a java main method too.

TestNG Test from Java Main Method

Let’s create a simple TestNG test class and a TestNG listener for our example.

package com.journaldev.main;

import org.testng.annotations.Test;

public class Test5 {

	public void test() {
		System.out.println("Running test method");

package com.journaldev.main;

import org.testng.ISuite;
import org.testng.ISuiteListener;

public class Test5SuiteListener implements ISuiteListener {

	public void onStart(ISuite suite) {
		System.out.println("TestNG suite default output directory = "+suite.getOutputDirectory());

	public void onFinish(ISuite suite) {
		System.out.println("TestNG invoked methods = " +suite.getAllInvokedMethods());


Now we want to run Test5 class tests and also want to add Test5SuiteListener listener to our TestNG test suite. We can easily do this using org.testng.TestNG class.

package com.journaldev.main;

import org.testng.TestNG;

public class TestNGMainClass {

	public static void main(String[] args) {
		TestNG testSuite = new TestNG();
		testSuite.setTestClasses(new Class[] { Test5.class });
		testSuite.addListener(new Test5SuiteListener());
		testSuite.setDefaultSuiteName("My Test Suite");
		testSuite.setDefaultTestName("My Test");


Notice that I am also changing TestNG reporting output directory, setting the test suite and test name.

Just run above class as java application and it should produce following output in the console.

TestNG suite default output directory = /Users/pankaj/temp/testng-output/My Test Suite
Running test method
TestNG invoked methods = [Test5.test()[pri:0, instance:com.journaldev.main.Test5@314c508a] 827084938]

My Test Suite
Total tests run: 1, Failures: 0, Skips: 0

You should also check for the HTML report generated by above program, it will be something like below image.

TestNG running tests from java main class

That’s all for running TestNG tests from java main method.

You can check out example code from our GitHub Repository.


  1. Mj says:

    This main method is not terminating even after testng execution.

  2. nitin sharma says:

    getting issue :

    Listener must be one of ITestListener, ISuiteListener, IReporter, IAnnotationTransformer, IMethodInterceptor or IInvokedMethodListener
    Usage: [options] The XML suite files to run
    Configuration failure policy (skip or continue)
    Output directory
    Number of threads to use when running data providers
    Comma-separated list of group names to exclude
    Comma-separated list of group names to be run
    JUnit mode
    Default: false
    List of .class files or list of class names implementing ITestListener
    or ISuiteListener
    Comma separated of test methods
    Default: []
    List of .class files or list of class names implementing IMethodSelector
    Mixed mode – autodetect the type of current test and run it with
    appropriate runner
    Default: false
    List of .class files or list of class names implementing
    Parallel mode (methods, tests or classes)
    Possible Values: [tests, methods, classes, instances, none, true, false]
    The port
    Extended configuration for custom report listener
    Default name of test suite, if not specified in suite definition file or
    source code
    Size of the thread pool to use to run suites
    Default: 1
    The list of test classes
    A jar file containing the tests
    Default name of test, if not specified in suitedefinition file or source
    The list of test names to run
    -testrunfactory, -testRunFactory
    The factory used to create tests
    Number of threads to use when running tests in parallel
    Whether to use the default listeners
    Default: true
    -log, -verbose
    Level of verbosity
    The full path to the xml file inside the jar file (only valid if
    -testjar was specified)
    Default: testng.xml

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors