How to run TestNG from command line?

TestNG allows to run the test suites from the command line (cmd). Here's a set of prerequisites that must be fulfilled in order to run a test suite from the command line −

  • testng.xml file should be created to define the test suites and the testing classes to execute.

  • All dependent jars should be available inside a project folder. It includes testing.jar, jcommander.jar and any other jars used in the test cases.

  • Path of bin or out folder where the .class files are stored after the compilation.

Approach/Algorithm to solve this problem

  • Step 1 − Create different testing classes having different @Test methods

  • Step 2 − Compile the class; it will create an out folder in IntelliJ and bin folder in Eclipse.

  • Step 3 − Place all the jar files in the lib folder.

  • Step 4 − Now create the testng.xml as given below.

  • Step 5 − Open the cmd.

  • Step 6 − Navigate to the project path using cd <project_path>

  • Step 7 − Run the following command−

java -cp <path of lib>; <path of out or bin folder>
org.testng.TestNG <path of testng>/testng.xml


The following code demonstrates how to run TestNG from the command line −


import org.testng.annotations.*;
import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
   // test case 1
   public void testCase1() {
      System.out.println("in test case 1");
   // test case 2
   public void testCase2() {
      System.out.println("in test case 2");
   public void beforeMethod() {
      System.out.println("in beforeMethod");
   public void afterMethod() {
      System.out.println("in afterMethod");
   public void beforeClass() {
      System.out.println("in beforeClass");
   public void afterClass() {
      System.out.println("in afterClass");
   public void beforeTest() {
      System.out.println("in beforeTest");
   public void afterTest() {
      System.out.println("in afterTest");
   public void beforeSuite() {
      System.out.println("in beforeSuite");
   public void afterSuite() {
      System.out.println("in afterSuite");


This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "" >

<suite name = "Suite1">
   <test name = "test1">
         <class name = "OrderofTestExecutionInTestNG"/>

Command to run

java -cp
org.testng.TestNG src/testng.xml

If the user didn't navigate to the testing project path using cd <project path>, then the complete path can be provided in the command as shown above. But, if the user is already in the testing project path, then the command can be modified as follows −

java -cp .\lib\*;.\out\production\TestNGProject
org.testng.TestNG src\testng.xml


in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTest
in afterSuite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0

Updated on: 12-Jan-2022

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started