Running TestNG Tests from Java Main Method With Examples

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.

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.

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.

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


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

By admin

Leave a Reply

%d bloggers like this: