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 {

	@Test
	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 {

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

	@Override
	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");
		testSuite.setOutputDirectory("/Users/pankaj/temp/testng-output");
		testSuite.run();
	}

}

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.

Comments

  1. nitin sharma says:

    getting issue :

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

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages